Java IO流相關知識代碼解析
一、IO流的分類
字符流
Reader
InputStreamReader(節(jié)點流)
BufferedReader(處理流)
Writer
OutputStreamWriter(節(jié)點流)
BufferedWriter(處理流)
PrintWriter
字節(jié)流
InputStream
FileInputStream(節(jié)點流)
BufferedInputStream(處理流)
ObjectInputStream(處理流)
PrintStream
OutputStream
FileOutputStream(節(jié)點流)
BufferedOutputStream(處理流)
ObjectOutputStream(處理流)
斷點處理的流
RandomAccessfile
二、IO流的用法
1、轉換流的用法
FileInputStream in = new FileInputStream(newFile(""));
Readerreader = new InputStreamReader(in);//字節(jié)轉字符
FileOutputStreamout = new FileOutputStream(newFile(""));
Writer writer = new OutputStreamWriter(out);//字符轉字節(jié)
2、對象序列化,對象需要實現(xiàn)Serializable接口
FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(object);//向指定文件寫入對象object
objectOutputStream.close();
FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();//讀取得到對象object
fileInputStream . lose();
3、斷點的運用
public class Copy extends Thread{
//可以利用多線程實現(xiàn)拷貝
longstart;
longend;
Filesorce;
Filetargetdir;
publicCopy() {
}
publicCopy(longstart,long end, File sorce, File targetdir) {
//利用構造方法傳遞需要拷貝的長度,拷貝開始位置,以及目標文件和源文件
super();
this.start= start;
this.end= end;
this.sorce= sorce;
this.targetdir= targetdir;
}
@Override
publicvoid run(){
try{
RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");
RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");
souceRaf.seek(start);
targetRaf.seek(start);
intlen= 0;
byte[]bs = new byte[1024];
longseek;
System.out.println(start+"---->"+end+this.getName());
while((len= souceRaf.read(bs))!=-1){
targetRaf.write(bs, 0, len);
seek= souceRaf.getFilePointer();
//獲取斷點位置
if(seek== end){
break;
}
}
targetRaf.close();
souceRaf.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
4、字節(jié)流的用法
public class Test_InputStream {
//利用字節(jié)流獲取文本文件內容,但是容易出現(xiàn)問題
/*
//可能出現(xiàn)int長度越界
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
String str = new String(b);
System.out.println(str);
}
*/
//可能出現(xiàn)亂碼
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
InputStream inputStream = new FileInputStream(file);
//統(tǒng)計每次讀取的實際長度
int len = 0;
//聲明每次讀取1024個字節(jié)
byte[] b = new byte[2];
StringBuffer sBuffer = new StringBuffer();
while((len=inputStream.read(b))!=-1){
sBuffer.append(new String(b,0,len));
}
System.out.println(sBuffer.toString());
}
}
//利用字節(jié)流拷貝文件
public void copy(File sourceFile, File targetDir) {
//
FileInputStreamfileInputStream = null;
FileOutputStreamfileOutputStream = null;
fileInputStream= new FileInputStream(sourceFile);
FiletargetFile = new File(targetDir,sourceFile.getName());
fileOutputStream= new FileOutputStream(targetFile);
byte[]b = new byte[1024];
intlen = 0;
while((len= fileInputStream.read(b)) != -1) {
fileOutputStream.write(b, 0, len);
}
}
5、緩存字符流的用法
publicstatic void main(String[] args) throws IOException {
//緩存字符流實現(xiàn)寫入文件
InputStreamin = System.in;
Readerreader = new InputStreamReader(in);
BufferedReaderbr = new BufferedReader(reader);
BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));
Strings="";
while((s=br.readLine())!=null) {
bw.write(s);
bw.newLine();
bw.flush();
//字符流千萬不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
總結
以上就是本文關于Java IO流相關知識代碼解析的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
SpringBoot條件注解之@ConditionalOnClass等注解的使用場景分析
文章詳細介紹了SpringBoot中條件注解的體系,包括基本概念、@ConditionalOnClass等常用注解的工作原理和使用場景,文章還探討了條件注解的組合使用、實戰(zhàn)應用以及最佳實踐,幫助開發(fā)者更好地理解和應用條件注解,實現(xiàn)更靈活和智能的應用配置,感興趣的朋友一起看看吧2025-03-03

