(JAVA)IO流之读写单个字节和复制文本文件
生活随笔
收集整理的這篇文章主要介紹了
(JAVA)IO流之读写单个字节和复制文本文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package IODemo;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;/*** @author Alina* @date 2021年10月15日 8:19 下午* read()方法特點* 1.每次只讀取一個文件* 2.只運行一次,自動向下讀取* 3.讀取到文件末尾,返回負數* 4.讀取整個文件時,利用read()返回 -1 的特點,進行循環*/
public class FileInputStreamDemo {public static void main(String[] args) {try {method_copy();} catch (IOException e) {e.printStackTrace();}}public static void method_read() throws IllegalAccessError, IOException {FileInputStream fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt" );int x = fis.read();System.out.println(x);x = fis.read();System.out.println(x);fis.close();}public static void method_readsum()throws IllegalAccessError, IOException{FileInputStream fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");int y = 0 ;while ((y =fis.read()) != -1){System.out.print((char)y);}fis.close();}public static void method_copy() throws IOException ,IllegalAccessError{FileInputStream fis = null;FileOutputStream fos = null;try{int bytes = 0;fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fos = new FileOutputStream("src/IODemo/test.txt");while ((bytes=fis.read()) != -1){fos.write(bytes);}}catch (IOException e){e.printStackTrace();throw new RuntimeException("復制失敗");}finally{try{if(fos != null){fos.close();}}catch (IOException e ){e.printStackTrace();throw new RuntimeException("復制失敗");}finally {try{if((fis!=null)){fis.close();}}catch (IOException e ){e.printStackTrace();throw new RuntimeException("復制失敗");}}}}
}
總結
以上是生活随笔為你收集整理的(JAVA)IO流之读写单个字节和复制文本文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这些免费模板,让Excel轻松制作精美图
- 下一篇: PLC编程从入门到精通视频教程【副业学习