Java 文件操作

来源:互联网 发布:js url encode utf8 编辑:程序博客网 时间:2024/06/14 05:17

//自我复习,Java 文件操作

import java.awt.List;import java.io.BufferedOutputStream;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.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.util.ArrayList; public class FileOperation {boolean createFile(String filePath){boolean result = false;File file = new File(filePath);if(!file.exists()){try {result = file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return result;}boolean deleteFile(String pathname){boolean result = false;File file = new File(pathname);if(file.exists() && file.isFile()){result = file.delete();}return result;}String readFileByByte(String pathname){File file = new File(pathname);FileInputStream fileInputStream = null;if(!file.exists() || !file.isFile()){return null;}StringBuffer content = new StringBuffer();byte[] temp = new byte[1024];try {fileInputStream = new FileInputStream(file);while(fileInputStream.read(temp) != -1){content.append(new String(temp));temp = new byte[1024];}fileInputStream.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();} return content.toString();}String readFileByChar(String filePath){File file = new File(filePath);if(!file.exists() || !file.isFile()){return null;}StringBuffer content = new StringBuffer();char[] temp = new char[1024];FileInputStream fileInputStream;try {fileInputStream = new FileInputStream(file);InputStreamReader inputStreamReader =  new InputStreamReader(fileInputStream, "utf-8");while(inputStreamReader.read(temp) != -1){content.append(new String(temp));temp = new char[1024];}fileInputStream.close();inputStreamReader.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return content.toString();}ArrayList<String> readFileByLine(String filePath){File file = new File(filePath);if(!file.exists() && !file.isFile()){return null;}ArrayList<String> content = new ArrayList<String>();try {FileInputStream fileInputStream = new FileInputStream(file);InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);BufferedReader reader = new BufferedReader(inputStreamReader);String lineContent = "";while( (lineContent = reader.readLine() ) != null){content.add(lineContent);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}return content;}void writeFileByByte(String filePath,String content){File file = new File(filePath);FileOutputStream fos = null;synchronized (this) {try {fos = new FileOutputStream(file);fos.write(content.getBytes());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();} finally{}try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}void writeFileByBufferedOutputStream(String filePath, String content){File file = new File(filePath);FileOutputStream fos = null;BufferedOutputStream bos = null;try {fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(content.getBytes());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO: handle exceptione.printStackTrace();} finally{try {fos.close();bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/* *  InputStreamReader  An InputStreamReader is a bridge from byte streams to character streams *  OutputStreamWriter An OutputStreamWriter is a bridge from character streams to byte streams * */void writeFileByBufferedWrite(String filePath){File file = new File(filePath);try {BufferedWriter bw = new BufferedWriter(new FileWriter(file));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args)  {}}


0 0
原创粉丝点击