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,FileInputStream、FileOutputStream字节流一般性操作
猜你喜欢
转载自blog.csdn.net/younow22/article/details/113408051
今日推荐
周排行