现在的位置: 首页 > 综合 > 正文

java 关于文件的读写操作

2018年05月28日 ⁄ 综合 ⁄ 共 4314字 ⁄ 字号 评论关闭

今天在此做一个总结,关于文件的读写操作:

package com.phj.file.read;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.util.Scanner;

public class ObjectFileReadAndWrite {

	/**
	 * @author feihong
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String path = "D:/TestFile/MathUtil.java";
		// ReadByByte(path);
		// ReadByBytes(path);
		// ReadByChars(path);
		// ReadByLine(path);
		File file = new File(path) ;
		readFromFile(file);
		// writeFile();
		// writeFileToMemory(path);
		// writeFile2(path);
		 writeFile3();
//		File file = new File("D:" + File.separator + "test");
//		printFile(file);
//		deleFile(file);
		double num = 12.213213;
		System.out.printf("%f", num);
		System.out.println("操作成功!");
	}

	public static void ReadByByte(String filePath) throws Exception {

		File file = new File(filePath);
		InputStream is = new FileInputStream(file);
		int temp;
		while ((temp = is.read()) != -1) {
			System.out.write(temp);
		}

		File file1 = new File("D:/test.txt");
		OutputStream os = new FileOutputStream(file1);
		os.write(is.read());

		is.close();
		os.close();
	}

	public static void ReadByBytes(String filePath) throws Exception {
		File file = new File(filePath);
		InputStream is = new FileInputStream(file);
		byte[] temp = new byte[1024];
		int flag = 0;
		is.available();
		while ((flag = is.read(temp)) != -1) {
			System.out.write(temp, 0, flag);
		}
		is.close();
	}

	public static void ReadByChar(String filePath) throws Exception {
		File file = new File(filePath);
		Reader reader = new FileReader(file);
		int temp = 0;
		while ((temp = reader.read()) != -1) {
			if ((char) temp != '\r') {
				System.out.print((char) temp);
			}

		}
		reader.close();
	}

	public static void ReadByChars(String filePath) throws Exception {
		File file = new File(filePath);
		Reader reader = new FileReader(file);
		char[] temp = new char[1024];
		int length = reader.read(temp);
		String str = new String(temp, 0, length);
		System.out.println(str);
		reader.close();
	}

	public static void ReadByLine(String filePath) throws Exception {
		File file = new File(filePath);
		BufferedReader br = new BufferedReader(new FileReader(file));
		String str = null;
		while ((str = br.readLine()) != null) {
			System.out.println(str);
		}
	}

	public static void readFromFile(File file) throws Exception{
		Scanner scan = new Scanner(file) ;
		StringBuffer sb = new StringBuffer();
		while(scan.hasNext()){
			sb.append(scan.next()).append("\n");
		}
		System.out.println(sb);
	}
	
	// 输入即返回
	public static void writeFile() throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter a line: ");
		boolean flag = true;
		while (flag) {
			System.out.println(br.readLine().concat("====="));
			flag = true;
		}
		br.close();
	}

	public static void writeFileToMemory(String filePath) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(filePath));
		String temp, file = null;
		while ((temp = br.readLine()) != null) {
			file += temp + '\n';
		}
		br.close();
		StringReader sr = new StringReader(file);
		int csr = 0;
		while ((csr = sr.read()) != -1) {
			System.out.print((char) csr);
		}
		sr.close();
	}

	// 文件写入
	public static void writeFile2(String filePath) throws Exception {

		BufferedReader br = new BufferedReader(new FileReader(filePath));
		String temp, file = null;
		while ((temp = br.readLine()) != null) {
			file += temp + '\n';
		}
		br.close();

		BufferedReader brWrite = new BufferedReader(new StringReader(file));
		PrintWriter pwOut = new PrintWriter(new BufferedWriter(new FileWriter(
				"D:/IODemo.txt")));
		int lintCount = 1;
		String s = "";
		try {
			while ((s = brWrite.readLine()) != null) {
				pwOut.println(lintCount + "--->" + s);
				lintCount++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		pwOut.close();
	}

	public static void writeFile3() throws Exception {      //这个操作就相当于是复制一份文件到另一个文件中
		File file = new File("D:/TestFile/MathUtil.java");
		FileOutputStream fos = null;
		FileInputStream fis = null;
		File wFile = new File("D:" + File.separator + "TestFile" + File.separator + "Hello.txt") ;
		try {
			fos = new FileOutputStream(wFile);
			fis = new FileInputStream(file);
			int temp = 0;
			while((temp = fis.read()) != -1){
				fos.write(temp);
			}
			fos.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void printFile(File file) {  //写出全部文件
		if (file != null) {
			if (file.isDirectory()) {
				File[] f = file.listFiles();
				if (f != null) {
					for (int i = 0; i < f.length; i++) {
						printFile(f[i]);
					}
				}
			} else {
				System.out.println(file);
			}
		}
	}

	public static void deleFile(File file) {  //删除相应文件夹以及相应全部文件
		if (file != null) {
			if (file.isDirectory()) {
				File[] f = file.listFiles();
				for (int i = 0; i < f.length; i++) {
					deleFile(f[i]);
				}
				file.delete();
			} else {
				file.delete();
			}
		}
	}
}

抱歉!评论已关闭.