其他分享
首页 > 其他分享> > File and Buffered Streams

File and Buffered Streams

作者:互联网

Access Files

Buffered Streams

try(BufferedReader br = new BufferedReader(new FileReader("file1.txt"))){
	int intVal;
	while(intVal = br.read()) >= 0){
		char charVal = (char) intVal;
		// do something with charVal
	}
}

extra benefit of buffered streams: break line support
Line breaks vary across platform

Buffered streams add line break support

// 多行读写模板
void writeData(String[] data) throws IOException{
try(BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"))){
		for(String d: data){
			bw.write(d);
			bw.newLine();
		} 		
	}
}

void readData() throws IOException{
try(BufferedReader br = new BufferedReader(new FileReader("data.txt"))){
	String inValue;
	while((inValue= br.readLine()) != null){
		System.out.println(inValue);
	}
  }
}

标签:Buffered,File,streams,br,new,Streams,line,data
来源: https://blog.csdn.net/jzhong720/article/details/118441793