一、IO流的分類
字符流
Reader
InputStreamReader(節點流)
BufferedReader(處理流)
Writer
OutputStreamWriter(節點流)
BufferedWriter(處理流)
PrintWriter
字節流
InputStream
FileInputStream(節點流)
BufferedInputStream(處理流)
ObjectInputStream(處理流)
PrintStream
OutputStream
FileOutputStream(節點流)
BufferedOutputStream(處理流)
ObjectOutputStream(處理流)
斷點處理的流
RandomAccessfile
二、IO流的用法
1、轉換流的用法
FileInputStream in = new FileInputStream(newFile("")); Readerreader = new InputStreamReader(in);//字節轉字符FileOutputStreamout = new FileOutputStream(newFile("")); Writer writer = new OutputStreamWriter(out);//字符轉字節2、對象序列化,對象需要實現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{//可以利用多線程實現拷貝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、字節流的用法
public class Test_InputStream {//利用字節流獲取文本文件內容,但是容易出現問題/* //可能出現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); }*///可能出現亂碼public static void main(String[] args) throws IOException {File file = new File("C://Users//lx//Desktop//test//33.txt");InputStream inputStream = new FileInputStream(file);//統計每次讀取的實際長度int len = 0;//聲明每次讀取1024個字節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());}}//利用字節流拷貝文件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 {//緩存字符流實現寫入文件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流相關知識代碼解析的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!