日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java流读写_java流概述以及文件读写示例

發(fā)布時間:2025/3/12 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java流读写_java流概述以及文件读写示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。