import java.io.File;
public class Demo {
public static void main(String[] args) {
//File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的File实例
File f1 = new File("D:\\JAVA\\java.txt");
System.out.println(f1);
//File(String parent,String child) 从路径名字字符串和子路径名字字符串创建新的File实例
File f2 = new File("D:\\JAVA","java.txt");
System.out.println(f2);
//File(File parent,String child)从父抽象路径名和子路径名字字符串创建新的File实例
File f3 = new File("D:\\JAVA");
File f4 = new File(f3,"java.txt");
System.out.println(f4);
}
}
public boolean createNewFile() 当具有该名称的文件不存在时,创建一个由该抽象路径命名的新空文件
public boolean mkdir() 创建由此路径名命名的目录
punlic boolean mkdirs() 创建由此抽象路径名命名的目录,包括任何必须但不存在的父目录
import java.io.File;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
//在D:\\JAVA目录下创建一个文件java.txt
File f1 = new File("D:\\JAVA\\java.txt");
System.out.println(f1.createNewFile());
//在E:\\JAVA目录下创建一个目录JavaEE
System.out.println("----------------");
File f2 = new File("D:\\JAVA\\JAVAEE");
System.out.println(f2.mkdir());
//在E:\\JAVA目录下创建一个多级目录javaWEB\\HTML
System.out.println("----------------");
File f3 = new File("D:\\JAVA\\javaWEB\\HTML");
System.out.println(f3.mkdirs());
}
}
import java.io.File;
public class Demo {
public static void main(String[] args) {
// 1. public boolean isDiectory() 测试此抽象路径名表示的File是否为目录
// 1. public boolean isFiel() 测试此抽象路径名表示的Fiel是否为文件
// 3. public boolean exists() 测试此抽象路径名表示的File是否存在
File f1 = new File("D:\\JAVA\\java.txt");
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
System.out.println(f1.exists());
System.out.println("---------------------------");
// 4. public String getAbsolutePath() 返回此抽象路径名转换为路径名字字符串
// 5. public String getPath() 将此抽象路径名转换为路径名字符串
// 6. public String getName() 返回由此抽象路径名表示的文件或目录的名称
System.out.println(f1.getAbsolutePath());
System.out.println(f1.getPath());
System.out.println(f1.getName());
// 7. public String[] list() 返回此抽象路径名表示的目录中的文件和目录的名称字符串数组
// 8. public File[] listFiles() 返回此抽象路径名表示的目录中的文件和目录的File对象数组
File f2 = new File("D:\\JAVA");
String[] list = f2.list();
System.out.println(list);
for (String li : list) {
System.out.println(li);
}
System.out.println("---------------------");
File[] files = f2.listFiles();
for (File filelist : files) {
// System.out.println(filelist);
if (filelist.isFile()) {
System.out.println(filelist.getName());
}
}
}
}
/*
给定一个路径(E:\\JAVA),请通过递归完成遍历该目录下的所有内容,并把所有文件的绝对路径输出到控制台
思路:
根据给定路径创建一个File对象
定义一个方法用于获取给定目录下的所有内容,参数为第一步创建的File对象
获取给定的File目录下所有的文件或者目录的File数组
遍历该File数组,得到每一个File对象
判断该File对象是否是目录
是:递归调用
不是:获取绝对路径输出在控制台
调用方法
*/
import java.io.File;
public class Demo {
public static void main(String[] args) {
File fiel = new File("D:\\JAVA");
getAllFilePath(fiel);
}
public static void getAllFilePath(File file){
File[] files = file.listFiles();
if (files != null){
for (File i : files){
if (i.isDirectory()){
getAllFilePath(i);
}else{
System.out.println(i.getAbsolutePath());
}
}
}
}
}
/*
FileOutputStream(String name):创建文件输出流以指定的名称写入文件
做了三件事:
调用系统功能创建了文件
创建了字节输出流对象
让字节输出流对象指向创建好的文件
void write(int b):将指定的字节写入此文件输出流
最后都要释放资源
void colse():关闭此文件输出流并释放与此相关联的任何系统资源
*/
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write(97);
fos.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("fos.txt");
//void write(int b) 将指定的字节写入此文件输出流
// fos.write(97);
// fos.write(98);
// fos.write(99);
// fos.write(100);
// fos.write(101);
//void write(byte[] b)将b.length字节从指定的字节数组写入此文件输出流一次写一个字节数组数据
byte[] bytes = "abcde".getBytes();
// fos.write(bytes);
//void write(byte[] b, int off, int len)将len字节从指定的字节数组开始,从偏移量off(索引位置)开始写入此文
fos.write(bytes,1,3);
}
}
fos.colse();
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("IO流\\fos.txt",true);
//如何换行
for (int i = 0; i < 10; i++){
fos.write("hello".getBytes());
//如何换行
fos.write("\r\n".getBytes());
}
//释放资源
fos.close();
}
}
try{
可能出现异常的代码
}catch(异常类名 变量名){
异常的处理代码;
}finally{
执行所有清除操作;
}
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) {
FileOutputStream fos = null;
try{
fos = new FileOutputStream("IO流\\fos.txt");
fos.write("hello".getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
if (fos != null){
try{
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
package 字节流.字节缓冲流;
import java.io.*;
/*
* BufferedOutputStream
* * BufferedInputStream
* 构造方法:
* 字节缓冲输出流:BufferedOutputStream(OutputStream out)
* 字节缓冲输入流:BufferedInputStream(InputStream in)
*/
public class Demo {
public static void main(String[] args) {
//BufferedOutputStream
/* BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
if (bos != null){
try {
bos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}*/
//BufferedInputStream
//读一
/* BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("bos.txt"));
int len;
while((len = bis.read()) != -1){
System.out.print((char)len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (bis != null){
try {
bis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}*/
//读二
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("bos.txt"));
byte [] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1){
System.out.print(new String(b,0,len));
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.*;
public class Demo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
//缓冲字节流一次读取一个字节
// method(); // 50 ms
//缓冲字节流一次读取一个字节数组
method2(); // 2 ms
//基本字节流一次读取一个字节数组
// method3(); // 20 ms
//基本字节流一次读取一个字节
// method4(); // 6000 ms
long end = System.currentTimeMillis();
System.out.println(end - start + "ms");
}
//缓冲字节流一次读取一个字节
public static void method() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("D:\\JAVA\\NBA.qlv"));
bos = new BufferedOutputStream(new FileOutputStream("D:\\JAVA\\JAVAEE\\NBA.qlv"));
int len;
while ((len = bis.read()) != -1) {
bos.write(len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis == null || bos == null) {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//缓冲字节流一次读取一个字节数组
public static void method2() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("D:\\JAVA\\NBA.qlv"));
bos = new BufferedOutputStream(new FileOutputStream("D:\\JAVA\\JAVAEE\\NBA.qlv"));
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis == null || bos == null) {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//基本字节流一次读取一个字节数组
public static void method3() {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\JAVA\\NBA.qlv");
fos = new FileOutputStream("D:\\JAVA\\JAVAEE\\NBA.qlv");
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos == null || fis == null) {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//基本字节流一次读取一个字节
public static void method4() {
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\JAVA\\NBA.qlv");
fos = new FileOutputStream("D:\\JAVA\\JAVAEE\\NBA.qlv");
int len;
while ((len = fis.read()) != -1) {
fos.write(len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos == null || fis == null) {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
由于字节流操作中文不是特别方便,所以Java就提供了字符流
编码:
解码:
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//编码
// OutputStreamWriter?(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
// OutputStreamWriter?(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
//默认编码
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));
osw.write("中国");
osw.close();
//自定义编码
OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("osw2.txt"), "GBK");
osw2.write("中华人民共和国");
osw2.close();
//解码
// InputStreamReader?(InputStream in) 创建一个使用默认字符集的InputStreamReader。
// InputStreamReader?(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
//默认编码
InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));
int ch;
while ((ch = isr.read()) != -1){
System.out.print((char) ch);
}
isr.close();
System.out.println();
//自定义编码
InputStreamReader isr2 = new InputStreamReader(new FileInputStream("osw2.txt"),"GBK");
int len;
char [] c = new char[1024];
while((len = isr2.read(c)) != -1){
System.out.println(new String(c,0,len));
}
isr2.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Demo {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));
//void write(int c) 写一个字符
osw.write(97);
osw.write(98);
//void write(char[] cbuf)写入一个字符数组
// char [] c = {‘a‘,‘b‘,‘c‘,‘d‘};
// osw.write(c);
//void write(char[] cbuf, int off, int len)写入字符数组的一部分
// char [] c = {‘a‘,‘b‘,‘c‘,‘d‘};
// osw.write(c,1,2);
//void write(String str)写一个字符串
// osw.write("hello");
//void write(String str, int off, int len)写入字符串的一部分
// osw.write("hello",1,2);
//void flush()刷新流
osw.flush();
osw.close();//关闭流 释放资源 先刷新 后释放
}
}
```import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) throws IOException {
//int read()一次读一个字符数据
InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));
int ch;
while ((ch = isr.read()) != -1){
System.out.print((char)ch);
}
isr.close();
System.out.println("--------------");
//int read(char[] cbuf)一次读一个字符数组数据
InputStreamReader isr2 = new InputStreamReader(new FileInputStream("osw.txt"));
int ch2;
char [] c = new char[1024];
while((ch2 = isr2.read(c)) != -1){
System.out.print(new String(c,0,ch2));
}
isr2.close();
}
}
转换流的名字较长,而我们常见的操作都是按照本地默认编码实现的,为了简便书写,转换流提供了对应的子类
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
// BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
// bw.write("hello\r\n");
// bw.write("world");
// bw.close();
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
//一次读取一个字符数据
// int len;
// while((len = br.read()) != -1){
// System.out.print((char)len);
// }
//一次读取一个字符数组
int len;
char [] c = new char[1024];
while((len = br.read(c)) != -1){
System.out.print(new String(c,0,len));
}
br.close();
}
}
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
/* BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
for (int i = 0; i < 10; i++){
bw.write("hello"+i);
bw.newLine();
}
bw.close();*/
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
}
}
格式:
try(定义流对象){
可能出现异常的代码;
}catch(异常类 名变量名){
异常的处理代码;
}
自动释放资源
定义输入流对象;
定义输出流对象;
try(输入流对象;输出流对象){
可能出现异常的代码;
}catch(异常类 名变量名){
异常的处理代码;
}
自动释放资源
例如:
package 复制文件异常加入异常处理;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo {
public static void main(String[] args) {
}
//JDK9的改进
private static void method3() throws IOException{
FileReader fr = new FileReader("fr.txt");
FileWriter fw = new FileWriter("fw.txt");
try(fr;fw){
int len;
char [] chars = new char[1024];
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
}catch (IOException e){
e.printStackTrace();
}
}
//JDK7的改进
private static void method2(){
try(FileReader fr = new FileReader("fr.txt");
FileWriter fw = new FileWriter("fw.txt")){
int len;
char [] chars = new char[1024];
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
}catch (IOException e){
e.printStackTrace();
}
}
//try...catch...finally
private static void method01(){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("fr.txt");
fw = new FileWriter("fw.txt");
int len;
char [] chars = new char[1024];
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if (fr != null){
try{
fr.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
if (fw != null){
try{
fw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
对象序列化:就是将对象保存到磁盘中,或网络传输对象
这种机制就是使用一个字节序列表示一个对象,该字节序列包含:对象的类型、对象的数据和对象的存储的属性等信息字节序列写到文件之后,相当于文件中保存了一个对象的信息,反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化
ObjectOutputStream将Java对象的原始数据类型和图形写入OutputStream。 可以使用ObjectInputStream读取(重构)对象。 可以通过使用流的文件来实现对象的持久存储。 如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象。
构造方法:
序列化对象的方法:
注意:对象所属的类需要实现Serializable接口.不实现此接口将不会使任何状态序列化或反序列化(仅是一个标识接口,无需要重新的方法)
ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象。
构造方法:
反序列化对象的方法:
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 42L;
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Demo {
public static void main(String[] args) {
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt"))
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oos.txt"))){
Student student = new Student("张三",20);
oos.writeObject(student);
Object obj = ois.readObject();
Student s = (Student)obj;
System.out.println(s.getName() + "," + s.getAge());
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
}
mport java.util.Properties;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
Properties p = new Properties();
p.put("张三",20);
p.put("李四",18);
p.put("王五",19);
Set<Object> keys = p.keySet();
for (Object key : keys){
System.out.println(p.get(key));
}
}
}
import java.util.Properties;
import java.util.Set;
public class Demo2 {
public static void main(String[] args) {
//Objiec setProperty(String key,String value)
Properties p = new Properties();
p.setProperty("张三", "19");
p.setProperty("李四", "20");
p.setProperty("王五", "18");
System.out.println(p);
System.out.println("--------------");
//String getProperty(String key)
String zs = p.getProperty("张三");
System.out.println(zs);
System.out.println("--------------");
//Set<String> stringPropertyNames()
Set<String> keys = p.stringPropertyNames();
for (String key : keys) {
System.out.println(key);
}
System.out.println("------------------");
Set<String> names = p.stringPropertyNames();
for (String key : names) {
String values = p.getProperty(key);
System.out.println(key + "," + values);
}
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Demo {
public static void main(String[] args) throws IOException{
// mystore();
myload();
}
private static void mystore() throws IOException {
Properties p = new Properties();
p.setProperty("张三","20");
p.setProperty("李四","19");
p.setProperty("王五","18");
//void store(OutputStream out,String comments) comments描述
FileWriter fw = new FileWriter("IO流\\fw.txt");
p.store(fw,null);
fw.close();
}
private static void myload() throws IOException{
Properties p = new Properties();
FileReader fr = new FileReader("IO流\\fw.txt");
p.load(fr);
System.out.println(p);
}
}
原文:https://www.cnblogs.com/Hz-z/p/12891680.html