Java file operation related examples


Java file operation related examples


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileOperation {

	public static void main(String[] args) {
		// create a new directory
		File folderPath = new File("D:\\ReadFileTest\\FileOperation");
		try {
			if (!folderPath.exists()) {
				folderPath.mkdir(); // create a new directory
			}
		} catch (Exception e) {
			System.out.println("Error creating new directory");
			e.printStackTrace ();
		}

		// create a new file
		File filePath = new File("D:\\ReadFileTest\\FileOperation\\FileOperation.txt");
		try {
			if (!filePath.exists()) {
				filePath.createNewFile(); // create a new file
			}
		} catch (Exception e) {
			System.out.println("Error creating new file");
			e.printStackTrace ();
		}

		// // Delete Files
		// File delFilePath = new
		// File("D:\\ReadFileTest\\FileOperation\\FileOperation.txt");
		// try {
		// delFilePath.delete();
		// } catch (Exception e) {
		// System.out.println("Error deleting file");
		// e.printStackTrace ();
		// }

		// // Delete the empty folder (the folder will not be deleted if it is not empty)
		// File delFolderPath = new File("D:\\ReadFileTest\\FileOperation");
		// try {
		// delFolderPath.delete(); // delete empty folders
		// } catch (Exception e) {
		// System.out.println("Error deleting folder");
		// e.printStackTrace ();
		// }

		// // Delete all files (folders) under a folder
		// File delfile = new File("D:\\ReadFileTest\\FileOperation");
		// File[] files = delfile.listFiles();
		// System.out.println(files.length);
		// for (int i = 0; i < files.length; i++) {
		// if (!files[i].isDirectory()) {
		// files[i].delete();
		// }
		// }

		// FileWriter writes the file (direct write without buffering)
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(filePath);
			fileWriter.write("test2");
			fileWriter.close();
		} catch (IOException e) {
			System.out.println("Error writing file");
			e.printStackTrace ();
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace ();
				}
			}
		}

		// FileReader reads the file (can't read line by line, can only read with one or more characters specified)
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(filePath);
			char[] cbuf = new char[10];
			try {
				fileReader.read(cbuf);
			} catch (IOException e) {
				e.printStackTrace ();
			}
			System.out.println("cbuf == " + cbuf[0]); // "t"
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		} finally {
			try {
				fileReader.close();
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}

		// BufferedReader reads the file (can read line by line, only one or more characters can be read) (with a buffer of 8192 characters) (read data in the form of a character stream)
		try {
			fileReader = new FileReader(filePath);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String readerValue;

		try {
			while (true) {
				readerValue = bufferedReader.readLine();
				if (readerValue == null) {
					break;
				}
				System.out.println("readerValue == " + readerValue);
			}

		} catch (IOException e) {
			e.printStackTrace ();
		} finally {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}

		// BufferedWriter reads the file (can't read line by line, can only read with one or more characters) (with a buffer of 8192 characters)
		try {
			fileWriter = new FileWriter(filePath);
		} catch (IOException e) {
			e.printStackTrace ();
		}

		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		try {
			bufferedWriter.write("hello world !");
			bufferedWriter.newLine();
			bufferedWriter.newLine();
			bufferedWriter.write("!hello world !");
			bufferedWriter.write("!hello world !");
			// Use the methods in the buffer to flush the data to the destination file.
			bufferedWriter.flush();
			// Close the buffer and close the fw stream object
			bufferedWriter.close();
		} catch (IOException e) {
			e.printStackTrace ();
		}

		// FileInputStream reads the file (reads data in the form of a byte stream)
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(filePath);
			try {
				int readData = fileInputStream.read();
				System.out.println("readData == " + readData);
				byte[] b = new byte[2];
				fileInputStream.read(b);
				System.out.println("b == " + b[0]);
			} catch (IOException e) {
				e.printStackTrace ();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		} finally {
			try {
				fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}

		// FileOutputStream writes files (writes data in the form of byte streams)
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(filePath);
			byte[] buf = new byte[] { 96, 0x77, 96 };
			try {
				fileOutputStream.write(buf);
			} catch (IOException e) {
				e.printStackTrace ();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		} finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}

		// InputStreamReader byte stream to character stream conversion
		InputStreamReader inputStreamReader = null;
		try {
			fileInputStream = new FileInputStream(filePath);
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		}
		inputStreamReader = new InputStreamReader(fileInputStream);
		char[] charBuf = new char[10];
		try {
			inputStreamReader.read(charBuf);
			System.out.println("charBuf = " + charBuf[0]);
		} catch (IOException e) {
			e.printStackTrace ();
		} finally {
			try {
				inputStreamReader.close();
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}

		// OutputStreamWriter byte stream to character stream conversion
		OutputStreamWriter outputStreamWriter = null;
		try {
			fileOutputStream = new FileOutputStream(filePath);
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		}

		outputStreamWriter = new OutputStreamWriter(fileOutputStream);
		try {
			outputStreamWriter.write("OutputStreamWriter");
		} catch (IOException e) {
			e.printStackTrace ();
		} finally {
			try {
				outputStreamWriter.close();
			} catch (IOException e) {
				e.printStackTrace ();
			}
		}

	}
}



Reference text: http://www.cnblogs.com/chen111/archive/2012/06/16/2551997.html
Reference text: http://uujava0322.iteye.com/blog/1045876
Reference text: http://blog .csdn.net/miyuki0424/article/details/307904
Reference text: http://xyiyy.iteye.com/blog/361476
Reference text: http://blog.csdn.net/liuhenghui5201/article/details/8279557
Reference text : http://blog.sina.com.cn/s/blog_70e5bf6e0101dwsd.html
Reference text: http://blog.csdn.net/liuhenghui5201/article/details/8292552

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326950371&siteId=291194637