package com.heima.stream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class wode2 {
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
//demo();
//demo2();
//demo3();
//demo4();
//demo5();
//demo6();
//demo8();
//demo9();
}
private static void demo9() throws FileNotFoundException, IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream("963.txt"));
bos = new BufferedOutputStream(new FileOutputStream("jjj.txt",true));
int b ;
while((b = bis.read()) != -1){
bos.write(b);
}
}finally{
try{
if(bis != null)
bis.close();
}finally{
if(bos != null)
bos.close();
}
}
}
private static void demo8() throws FileNotFoundException, IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("963.txt"));
bos.write("你好吗,我很好!".getBytes());
bos.write("\r\n".getBytes());
bos.close();
}
private static void demo7() throws FileNotFoundException, IOException {
//BufferedInputStream bis = new BufferedInputStream(new FileInputStream("双元.jpg"));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy1.jpg"));
int b ;
while((b = bis.read()) != -1){
bos.write(b^3);
}
bis.close();
bos.close();
}
private static void demo6() throws FileNotFoundException, IOException {
File file = getFile();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
int b ;
while((b = bis.read() ) != -1){
bos.write(b);
}
bis.close();
bos.close();
BufferedInputStream s = new BufferedInputStream(new FileInputStream("计算机网络.doc"));
BufferedOutputStream t = new BufferedOutputStream(new FileOutputStream("t.doc"));
int c ;
while((c = s.read() ) != -1){
t.write(c);
}
s.close();
t.close();
}
public static File getFile(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个文件路径:");
while(true){
String s = sc.nextLine();
File file = new File(s);
if(!file.exists()){
System.out.println("文件路径不存在");
}else if(file.isDirectory()){
System.out.println("您录入的文件路径不存在,请重新录入:");
}else{
return file;
}
}
}
private static void demo5() throws FileNotFoundException, IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy1.jpg"));
int b;
//Random r = new Random(456);
//int a= r.nextInt();
while((b = bis.read()) != -1){
bos.write(b^123);
}
bis.close();
bos.close();
}
private static void demo4() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("963.txt");
FileOutputStream fos = new FileOutputStream("jjj.txt");
int n ;
while((n = fis.read()) != -1){
fos.write(n);
}
fis.close();
fos.close();
}
private static void demo3() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("jjj.txt");
byte[] arr = new byte[3];
int len;
while((len = fis.read(arr)) != -1){
System.out.println(new String(arr,0,len));
}
fis.close();
}
private static void demo2() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("text.txt");
FileOutputStream fos = new FileOutputStream("963.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] arr = new byte[1024];
int len;
while((len = bis.read(arr)) != -1){
bos.write(arr,0,len);
}
bis.close();
bos.close();
}
private static void demo() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("yyy.txt");
FileOutputStream fos = new FileOutputStream("123.txt");
byte[] arr = new byte[1024];
int b;
while((b = fis.read(arr)) != -1){
fos.write(arr,0,b);
}
fis.close();
fos.close();
}
}
|
|