一、文件
创建文件:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、IO流分类
image.png
三、节点流FileInputStream
文件字节输入流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readLen = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、节点流FileOutputStream
文件字节输出流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
FileOutputStream fileOutputStream =null;
try {
// 参数加true是追加内容,不加是覆盖
fileOutputStream = new FileOutputStream(filePath);
String str = "hello,world!";
fileOutputStream.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream .close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、节点流FileReader
文件字符输入流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
FileReader fileReader = null;
int readLen = 0;
char[] buf = new char[8];
try {
fileReader = new FileReader(filePath);
while ((readLen = fileReader.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、节点流FileWriter
文件字符输出流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(filePath);
fileWriter.write(" 你好北京~");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
六、处理流-BufferedReader
字符流输入流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
String line;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(filePath));
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七、处理流-BufferedWriter
字符流输出流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("哈哈哈");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
八、处理流-BufferedInputStream
字节输入流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
BufferedInputStream bufferedInputStream = null;
byte[] buff = new byte[1024];
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
while (bufferedInputStream.read(buff) != -1) {
System.out.println(new String(buff));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
九、处理流-BufferedOutputStream
字节输出流:
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.txt";
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
bufferedOutputStream.write("xxx".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
十、对象流-ObjectInputStream
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.dat";
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
System.out.println(objectInputStream.readObject()); // 这里返回的是object对象
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Cat implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
十一、对象流ObjectOutputStream
public class Home {
public static void main(String[] args) {
String filePath = "e:\\a.dat";
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(filePath));
oos.writeObject(new Cat("小黄"));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Cat implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 序列化的类必须实现Serializable接口
- 读写顺序要一致
- 序列化中的类建议添加
十二、Properties
Properties是专门用于读写配置文件的集合类。格式是key=value
常见方法:
- load 加载数据到properties对象
- list 将数据显示到指定设备
- getProperty(key) 根据key获取到value
- setProperty(key, value)设置键值对到properties对象
- store 将properties对象保存的键值对存储到配置文件中
//1. 创建 Properties 对象
Properties properties = new Properties();
//2. 加载指定配置文件
properties.load(new FileReader("src\\mysql.properties"));
//3. 把 k-v 显示控制台
properties.list(System.out);
//4. 根据 key 获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
网友评论