java流读写_java流概述以及文件读写示例
1. 先分清楚是字節(jié)流還是字符流。
字節(jié)流:InputStream OutputStream
字符流:Reader Writer
字符流與字節(jié)流的區(qū)別是讀取的單位長度不同,字節(jié)流讀8字節(jié),字符流讀16字節(jié),所以有中文時,就得用字符流。
2. 在字節(jié)/字符流基礎(chǔ)上,又分為節(jié)點流和處理流
節(jié)點流:直接和數(shù)據(jù)源相連。
例如:?FileInputStream??FileOutputStream(字節(jié)流)
FileReader? ?FileWriter(字符流)
處理流:顧名思義,就是處理在節(jié)點流上加了層處理,所以要有節(jié)點流才有處理流。
例如: BufferedInputStream??BufferedOutputStream(字節(jié)流)
BufferedReader? BufferedWriter(字符流)
3.?文件讀寫簡單示例
import java.io.*;public classFileRead {public static void main(String[] args) throwsFileNotFoundException {
String filename= "file.txt";
String file_path= System.getProperty("user.dir")+ File.separator + "src" + File.separator +filename;
File file= newFile(file_path);if (!file.exists()){try{//創(chuàng)建文件
file.createNewFile();
FileWriter fll= newFileWriter(file);
BufferedWriter bf= newBufferedWriter(fll);
String a= "hellohello";
bf.write(a+"\r\n"+"niuniu");
bf.flush();
bf.close();
}catch(IOException e) {
e.printStackTrace();
}
}else{
FileInputStream in= null;/** 字節(jié)流read*/
//try {//in = new FileInputStream(file);//int b = in.read();//while (b != -1){//System.out.println((char)b);//b = in.read();//}//} catch (FileNotFoundException e) {//e.printStackTrace();//} catch (IOException e) {//e.printStackTrace();//}finally {//try {//in.close();//} catch (IOException e) {//e.printStackTrace();//}
/** 字符流read*/
try{//直接上字符流
FileReader rd = newFileReader(file);
BufferedReader br0= newBufferedReader(rd);//從字節(jié)流轉(zhuǎn)為字符流
in = newFileInputStream(file);
InputStreamReader reader= newInputStreamReader(in);
BufferedReader br= newBufferedReader(reader);try{
String line;
line=br.readLine();while (line != null){
System.out.println(line);
line=br.readLine();
System.out.println(br0.readLine());
}
}catch(IOException e) {
e.printStackTrace();
}
}finally{try{
in.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
4. 隨機(jī)存取
/*
* 將內(nèi)容追加到文件末尾
*/
try{//打開一個隨機(jī)訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(System.getProperty("user.dir") + File.separator + "file.txt", "rw");//文件長度,字節(jié)數(shù)
long fileLength =randomFile.length();//將寫文件指針移到文件尾。
randomFile.seek(fileLength);//在文件末尾加上Task_ID
randomFile.writeBytes((","+Task_ID));
randomFile.close();
}catch(IOException e) {
e.printStackTrace();
}
/*
* 隨機(jī)訪問文件
*/
public?static?void?readFileByRandomAccess(String?fileName)?{
RandomAccessFile?randomFile?=?null;
try?{
//?打開一個隨機(jī)訪問文件流,按只讀方式
randomFile?=?new?RandomAccessFile(fileName,?"r");
//?文件長度,字節(jié)數(shù)
long?fileLength?=?randomFile.length();
//?讀文件的起始位置
int?beginIndex?=?(fileLength?>?4)???4?:?0;
//?將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]?bytes?=?new?byte[10];
int?byteread?=?0;
//?一次讀10個字節(jié),如果文件內(nèi)容不足10個字節(jié),則讀剩下的字節(jié)。
while?((byteread?=?randomFile.read(bytes))?!=?-1)?{
System.out.write(bytes,?0,?byteread);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally?{
if?(randomFile?!=?null)?{
try?{
randomFile.close();
}?catch?(IOException?e1)?{
}
}
}
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java流读写_java流概述以及文件读写示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html读取servlet,简单html
- 下一篇: 没有足够的值_元丰通宝值多少钱吗?市场价