Java的基本输入输出流

大数语言关于文件的读取和写入都大同小异,比如CPP文件输入输出 和接下来的Java(Java里面有众多包可以用,所有文件的输入输出也有很多,他们各有用途,具体参照API文档)。
首先认识到:
输入和输出的参照物是内存中的应用程序,
输入时,是将别的文件里的数据读取到程序中来;

输出时,是将程序里的数据写入到别的文件里去。


IO流①

/**
 * Java的基本输入输出流
 * @author Last D
 * @version 1.0
 */
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
 
public class FioTest {
	//读取文件
	static void FileRead () throws IOException {
		File f = new File("G:/MyProject/JAVA/FileIO/test.txt");
		FileInputStream fis = null;
		byte[] b = new byte[8];//缓存数组,提高读取效率
		try {
			fis = new FileInputStream(f);
			int data = -1;
			while ((data = fis.read(b)) != -1) {
				String s = new String(b,0,data);//打印读取到缓存数组里的所有数据
				System.out.print (s);
			}
		} catch (FileNotFoundException ex) {
				ex.printStackTrace();
		} finally {
 
			//当方法后面有抛出异常的话(如:throws IOException),就不需要 try…catch… 捕获异常了
			fis.close();
 
			/*//方法后面没有抛出异常的话,就用如下代码块捕获异常
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();   //打印异常
			}
			*/
		}
	}
 
	//写入文件
	static void FileWrite () throws IOException{
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入即将写入文件的数据:");
		String string = scanner.nextLine();
		scanner.close();
		FileOutputStream fos = null;
		try {
			//后面的 true 表示当文件里存在数据时,在已存在的数据后面直接追加写入
			//而当是 false 时会覆盖文件中已有的数据
			fos = new FileOutputStream("G:/MyProject/JAVA/FileIO/write.txt", true);
			fos.write(string.getBytes());
			} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
 
	public static void main(String[] args) throws IOException {
		FileRead();
		System.out.println();//换行
		FileWrite();
	}
}

IO流②

/**
 * 利用FileWriter和FileReader基本一样,但是是读取的字符,读取文本时显得更方便
 * @author Last D
 * @version 1.0
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
 
public class WriterReader {
 
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入需拷贝的源文件路径(包括文件名及其扩展名):\n");
		String srcPath = sc.nextLine();
		System.out.println("请输入拷贝后文件的放置路径(包括需要创建的文件名及其扩展名):\n");
		String decPath = sc.nextLine();
		File src = new File(srcPath);
		File dec = new File(decPath);
		dec.createNewFile();  //创建文件
		Copy (src, dec);
		sc.close();
	}
 
	static void Copy (File src,File dec) {
		FileReader fReader = null;
		FileWriter fWriter = null;
		int temp = 0;
		try {
			fReader = new FileReader(src);
			fWriter = new FileWriter(dec);
			while ((temp=fReader.read()) != -1) {
				fWriter.write(temp);
			}
		} catch (FileNotFoundException ex) {
			// TODO: handle exception
			ex.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				fWriter.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			try {
				fReader.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
 
}


    拷贝小程序,毕竟认识不深,有错在所难免~~
/**
 * 复制一个目录下的所有子文件到另一个目录下
 * @author Last D
 * @version 1.0
 */
 
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.Scanner;
 
public class Copy {
	static void CopyFile (File src,File dec) throws IOException {
		FileInputStream fis = null;
		BufferedInputStream bis = null;     //Java自带缓存数组,提高读写效率
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		//byte[] buffer = new byte[1024];   //设置缓存数组,提高读写效率
		int temp = 0;
		try {
			fis = new FileInputStream(src);
			bis = new BufferedInputStream(fis);
			fos = new FileOutputStream(dec);
			bos = new BufferedOutputStream(fos);
			while ((temp=bis.read()) != -1) {
				bos.write(temp);
			}
			/*//对应自己设置的缓存数组
			while ((temp=fis.read(buffer)) != -1) {
				fos.write(buffer,0,temp);
			}
			*/
		} catch (FileNotFoundException ex) {
			// TODO: handle exception
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			//
			//【处理流的关闭循序:后开的先关闭】
			//
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	static void CopyDir (File file1,File file2) throws IOException {
		File[] files = file1.listFiles();
		for (File f : files){
			if (f.isDirectory()){
				CopyDir(f, new File(file2,file1.getName()));
			}
			if (f.isFile()) {
				CopyFile(f, new File(new File(file2,file1.getName()),f.getName()));
			}
		}
	}
 
	public static void main(String[] args) throws IOException {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入需要复制的源文件路径:\n");
		String srcString = scanner.nextLine();
		File src = new File(srcString);
		System.out.println("请输入需要复制的目的文件路径:\n");
		String decString = scanner.nextLine();
		File dec = new File(decString);
                dec.createNewFile();//创建文件
		scanner.close();
		CopyDir(src,dec);
	}
}


更多请访问超哥的小栈



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值