//自我复习,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 block
e.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 block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.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 block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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 block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.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 block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally{}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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 block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally{
try {
fos.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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 block
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}