Table of contents
IO flow overview and classification
Edit FileWriter to write data
FileWriter method of writing data
Frequently Asked Questions about Writing Data with FileWriter
IOStream Overview and Classification
- IO streams are used to handle data transfer between devices
–文品复法,上传文目和下载文目
- IO flow classification
–Outflow——FileWriter
–Input flow——FileReader
Detailed explanation of the diagram:
Input stream: Read the text content, display it on the monitor, use it, and use FileWriter to operate it.
Output stream: If you want to update the text content, add some content to it and use FileReader to operate.
FileWriter writes data
- Construction method
–FileWriter(String fileName)
- member method
–void write(String str)
–void flush()——清缓冲
–void close()——关闭文品
package com.demo01;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* @author 无限嚣张(菜菜)
* 需求:从文件中写数据
*
* FileWriter(String fileName):传递一个文件名称
*
* 输出流写数据步骤:
* A:创建输出流对象
* B:调用输出流写数据的方法
* C:释放资源
*
*/
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
//创建一个文件对象
FileWriter fw = new FileWriter("c:\\zahng.txt");
/*
* 创建输出流对象做了哪些事情:
* A:调用系统资源创建一个文件
* B:创建输出流对象
* C:把输出流对象指向文件
*/
//调用输出流对象的写数据的方法
//写一个字符串数组
fw.write("IO流你好,HELLO");
//数据没有直接写到文件,其实写到了内存缓冲区
fw.flush();
//释放资源
//ͨ通知系统释放和该文件相关的资源
//关闭文件输出流
fw.close();
}
}
If it is closed: can I write again? After executing fw.close(), no more content can be written.
FileWriterHow to write data
- void write(String str): Write a string data
- void write(String str,int index,int len): Write part of the data in a string
- void write(int ch): write a character data
- void write(char[] chs): Write a character array
- void write(char[] chs,int index,int len): Write part of the data of a character array
package com.demo01;
import java.io.FileWriter;
import java.io.IOException;
/*
* void write(String str):写一个字符串数据
* void write(String str,int index,int len):写一个字符串中的一部分数据
* void write(int ch):写一个字符数据
* void write(char[] chs):写一个字符数组
* void write(char[] chs,int index,int len):写一个字符数组的一部分数据
*/
public class FileWriterDemo3 {
public static void main(String[] args) throws IOException {
//创建一个文件对象
FileWriter fw = new FileWriter("c://zy.txt");
//void write(String str):写一个字符串数组
fw.write("abcde");
fw.write("\r\n");
//void write(String str,int index,int len):写一个字符串中的一部分数据
fw.write("abcde",0,5);
fw.write("\r\n");
fw.write("abcde",1,3);
fw.write("\r\n");
//void write(int ch):void write(int ch):写一个字符数据
fw.write('a');
fw.write(97);
fw.write("\r\n");
//void write(char[] chs):写一个字符数组
char[] chs = {'a','b','c','d','e'};
fw.write(chs);
fw.write("\r\n");
//void write(char[] chs,int index,int len):写一个字符数组的一部分数据
fw.write(chs,0,5);
fw.write("\r\n");
fw.write(chs,2,3);
fw.write("\r\n");
//关闭文件
fw.close();
}
}
think?????
FileWriterFrequently Asked Questions about Writing Data
1. How to achieve line wrapping of data?
windows:\r\n
linux:\n
mac:\r
2. How to implement additional writing of data?
FileWriter fw = new FileWriter("c.text");
package com.demo01;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo4 {
public static void main(String[] args) throws IOException {
//FileWriter fw = new FileWriter("c://c.txt");
FileWriter fw = new FileWriter("c://c.txt",true); //表示追加输入,默认是false
for(int x=0; x<10; x++) {
fw.write("hello world"+x);
fw.write("\r\n");
}
//关闭文件
fw.close();
}
}
FileReaderNumber of instructions
- Construction method
–FileReader(String fileName)
- member method
–int read()
–int read(char[] cbuf)
Reading without parameters
package com.demo02;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
* 需求:从文件中读数据并显示到控制台
* 读数据——输入流——FileReader
*
* FileReader:
* FileReader(String fileName):传递文件名称
*
* 输入流读文件的步骤
* A:创建输入流对象
* B:调用输入流对象读数据的方法
* C:释放资源
*
* java.io.FileNotFoundException: fr.txt (系统不指定文件)
*/
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
//创建输入流对象
FileReader fr = new FileReader("c://zy.txt");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
//释放
fr.close();
}
}
Data reading with parameters
package com.demo02;
import java.io.FileReader;
import java.io.IOException;
/*
* 输入流读数据的步骤
* A:创建输入流对象
* B:调用输入流对象的读数据的方法
* C:释放资源
*/
public class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
// 创建文件数据对象
FileReader fr = new FileReader("c://zy.txt");
char[] chs = new char[1024]; //创建数组对象
int len;
//1:fr.read(chs)
//2:len=fr.read(chs)
//3:len != -1
while((len=fr.read(chs))!=-1) {
//System.out.println(new String(chs,0,len));
System.out.print(new String(chs,0,len));
}
//释放数据
fr.close();
}
}