package edu.zhku.javase.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {
private static String path="F://LearningDirection/JavaSE/javase/readhello.txt";
private static String path1="F://LearningDirection/JavaSE/javase/readhello1.txt";
public static String readFileContent() {
IOUtils ioUtils = new IOUtils();
String filePath = path;
String reslut = ioUtils.readFile( filePath ) ;
System.out.println( reslut );
return reslut;
}
public String readFile( String filePath ){
FileInputStream fis=null;
String result = "" ;
try {
fis = new FileInputStream( filePath );
int size = fis.available() ;
byte[] array = new byte[size];
fis.read( array ) ;
result = new String(array);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result ;
}
public static void writeContentToFile(String[] args) {
IOUtils a2 = new IOUtils();
String filePath = path;
String content = "今天是2017/1/9,天气很好" ;
a2.writeFile( filePath , content ) ;
}
public void writeFile( String filePath , String content ){
FileOutputStream fos = null ;
try {
fos = new FileOutputStream( filePath );
byte[] array = content.getBytes() ;
fos.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
IOUtils a2 = new IOUtils();
String filePath1 = path;
String filePath2 = path1;
a2.copyFile( filePath1 , filePath2 );
}
public void copyFile( String filePath_old , String filePath_new){
FileInputStream fis=null ;
FileOutputStream fout = null ;
try {
fis = new FileInputStream( filePath_old );
int size = fis.available() ;
byte[] array = new byte[size];
fis.read( array ) ;
fout = new FileOutputStream( filePath_new ) ;
fout.write( array );
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
if ( fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( fout != null ) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}