目录
1. 创建文件
//方式一
public void create1() {
String path = "D:\\test1.txt";
File file = new File(path);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//方式二
public void create2() {
File filePath = new File("D:\\");
String file = "test2.txt";
File file1 = new File(filePath, file);
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//方式三
public void create3() {
String path = "D:\\";
String children = "text3.txt";
File file = new File(path, children);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
2. 字节输入流
2.1 使用FileInputStream
public static void main(String[] args) throws IOException {
String path = "D:\\test.txt";
int readDate = 0;
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(path);
while ((readDate = fileInputStream.read()) != -1) {
System.out.print((char) readDate);
}
fileInputStream.close();
}
一次读取多个字符
public static void main(String[] args) throws IOException {
String path = "D:\\test.txt";
FileInputStream fileInputStream = null;
byte[] bytes = new byte[8];
int readLen = 0;
fileInputStream = new FileInputStream(path);
while ((readLen = fileInputStream.read(bytes)) != -1) {
System.out.print(new String(bytes,0,readLen));
}
fileInputStream.close();
}
3. 字节输出流
3.1 使用FileOutStream
FileOutputStream fileOutputStream = null;
String filePath = "d:\\a.txt";
try {
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();
}
}
此方式会覆盖原有的内容
fileOutputStream = new FileOutputStream(filePath);
此方式不会覆盖原有的内容,会在末尾进行追加
fileOutputStream = new FileOutputStream(filePath,true);
4. 文件拷贝
String filePath = "f:\\settings2.xml";
String nweFilePath = "f:\\newSettings2.xml";
byte[] buf = new byte[1024];
int bufLen = 0;
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
fileOutputStream = new FileOutputStream(nweFilePath);
while ((bufLen = fileInputStream.read(buf)) != -1) {
fileOutputStream.write(buf,0,bufLen);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
5. 字符输入流
5.1 FileReader
String path = "d:\\a.txt";
FileReader fileReader = null;
char[] chars = new char[8];
int charsLen = 0;
try {
fileReader = new FileReader(path);
while ((charsLen = fileReader.read(chars)) != -1) {
System.out.print(new String(chars,0,charsLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
6. 字符输出流
6.1 FileWriter
FileWriter fileWriter = null;
String path = "d:\\b.txt";
String content = "hello 你好";
try {
fileWriter = new FileWriter(path,true);
fileWriter.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
7. 节点流
7.1 BufferedReader
public static void main(String[] args) throws Exception {
String path = "d:\\a.txt";
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
String strLen = "";
//按行读取
while ((strLen = bufferedReader.readLine()) != null){
System.out.println(strLen);
}
bufferedReader.close();
}
7.2 BufferedWriter
public static void main(String[] args) throws IOException {
String path = "d:\\c.txt";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
bufferedWriter.write("123");
//换行
bufferedWriter.newLine();
bufferedWriter.write("456");
bufferedWriter.close();
}
7.3 使用Buffered复制文件
public static void main(String[] args) throws IOException {
String copyPath = "d:\\c.txt";
String newPath = "d:\\d.txt";
String strLen = "";
BufferedReader bufferedReader = new BufferedReader(new FileReader(copyPath));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newPath));
while ((strLen = bufferedReader.readLine()) != null){
bufferedWriter.write(strLen);
//换行
bufferedWriter.newLine();
}
bufferedReader.close();;
bufferedWriter.close();
}
8. Object实现序列化(保存数据的类型和数据)
8.1 ObjectInputStream
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException {
String path = "d:\\o.dat";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));
objectOutputStream.writeInt(100);
objectOutputStream.writeChar('1');
objectOutputStream.writeBoolean(true);
objectOutputStream.writeUTF("123");
objectOutputStream.writeObject(new Dog("旺财",5));
objectOutputStream.close();
}
}
class Dog implements Serializable{
public String name;
public int age;
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
8.2 ObjectOutStream
public static void main(String[] args) throws IOException, ClassNotFoundException {
String path = "d:\\o.dat";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
System.out.println(ois.readInt());
System.out.println(ois.readChar());
System.out.println(ois.readBoolean());
System.out.println(ois.readUTF());
Object dog = ois.readObject();
System.out.println("运行类型" + dog.getClass());
System.out.println(dog);
ois.close();
}
9. 转换流
9.1 InputStreamReader
public static void main(String[] args) throws IOException {
String path = "d:\\a.txt";
InputStreamReader isr = new InputStreamReader(new FileInputStream(path), "gbk");
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
System.out.println(line);
br.close();
}
9.2 OutputStreamWriter
public static void main(String[] args) throws IOException {
String path = "d:\\a1.txt";
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path),"gbk");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("123456");
bw.close();
}
10. 打印流 只有输出流没有输入流
10.1 PrintStream
public static void main(String[] args) throws IOException {
PrintStream ps = System.out;
ps.write("123".getBytes());
//修改打印的位置
System.setOut(new PrintStream("d:\\a3.txt"));
System.out.println("123");
}