admin 管理员组文章数量: 887007
Java,FileInputStream、FileOutputStream字节流一般性操作
package xyz.zlqhe.www;import java.io.*;
import java.util.Scanner;public class MainTest {public static void main(String[] args) throws IOException {//拷贝://Demo_01(); //逐个字节//Demo_02(); //大数组//Demo_03(); //小数组//Demo_04(); //Buffered//Demo_05(); //拷贝读取中文,标准异常处理代码//Demo_06(); //1.7版本之后标准异常处理//Demo_07(); //文件加密Demo_08(); //记事本模板}public static void Demo_08() throws IOException{Scanner sc = new Scanner(System.in);System.out.print("请输入记事本名字:");String s = sc.nextLine() + ".txt";System.out.println("此为记事本,请输入:");FileOutputStream fileOutputStream = new FileOutputStream(s);while(true){String s2 = sc.nextLine();if(s2.equals("QUIT")) break;fileOutputStream.write(s2.getBytes());fileOutputStream.write("\r\n".getBytes());}}private static void Demo_07() throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream("AfterBuf.mp3"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("AfterBuf2.mp3"));int b;while((b = bis.read()) != -1) {bos.write(b ^ 123);}bis.close();bos.close();}private static void Demo_06() throws IOException{FileOutputStream fout = new FileOutputStream("bbb.txt");String s = "你好世界";fout.write(s.getBytes());fout.close();//标准异常处理代码try(FileInputStream fis = new FileInputStream("bbb.txt");FileOutputStream fos = new FileOutputStream("copy_bbb.txt");){byte[] arr = new byte[4];int length;while ((length = fis.read(arr)) != -1) {fos.write(arr, 0, length);}}}private static void Demo_05() throws IOException{FileOutputStream fout = new FileOutputStream("bbb.txt");String s = "你好世界";fout.write(s.getBytes());fout.close();//标准异常处理代码FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("bbb.txt");fos = new FileOutputStream("copy_bbb.txt");byte[] arr = new byte[4];int length;while ((length = fis.read(arr)) != -1) {fos.write(arr, 0, length);}}finally {try{if(fis != null) fis.close();}finally {if(fos != null) fos.close();}}}private static void Demo_04() throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream("一腔诗意喂了狗.mp3"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("0"));int b;while ((b=bis.read())!=-1) {bos.write(b);}bis.close();bos.close();}private static void Demo_03() throws IOException {FileInputStream fileInputStream = new FileInputStream("一腔诗意喂了狗.mp3");FileOutputStream fileOutputStream = new FileOutputStream("O.mp3");byte[] arr = new byte[1024 * 8];int length;while((length = fileInputStream.read(arr)) != -1) {fileOutputStream.write(arr,0,length);}fileInputStream.close();fileOutputStream.close();}private static void Demo_02() throws IOException{FileInputStream fileInputStream = new FileInputStream("一腔诗意喂了狗.mp3");FileOutputStream fileOutputStream = new FileOutputStream("o.mp3");byte[] fis = new byte[fileInputStream.available()];fileInputStream.read(fis);fileOutputStream.write(fis);fileInputStream.close();fileOutputStream.close();}private static void Demo_01() throws IOException{FileInputStream fileInputStream = new FileInputStream("a.jpg");FileOutputStream fileOutputStream = new FileOutputStream("b.jpg");int a;while((a = fileInputStream.read()) != -1){fileOutputStream.write(a);}fileInputStream.close();fileOutputStream.close();}
}
本文标签: java FileInputStreamFileOutputStream字节流一般性操作
版权声明:本文标题:Java,FileInputStream、FileOutputStream字节流一般性操作 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1731107715h1442595.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论