package com.chinasofti.javase20160819;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class IoTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr=readFile("D:\\MyFT\\demo\\javase20160819\\src\\com\\chinasofti\\javase20160819\\test.txt");
for(int v:arr)
System.out.print((char)v);
writeFile("D:\\MyFT\\demo\\javase20160819\\src\\com\\chinasofti\\javase20160819\\test1.txt",arr);
String str = "affjaisfi和还是滴啊会是个";
writeFile("D:\\MyFT\\demo\\javase20160819\\src\\com\\chinasofti\\javase20160819\\test1.txt",str);
}
/**
* 读文件,返回int[],文件中的字符的编码
* @param panth
*/
private static int[] readFile(String panth){
File dir=new File(panth);
FileInputStream fil=null;
InputStreamReader inStr=null;
BufferedReader buffR=null;
int length = (int)dir.length();
int[] arr = new int[length];
try {
fil = new FileInputStream(dir);
inStr =new InputStreamReader(fil,"GBK");//中转流,把字节流转换成字符流
buffR = new BufferedReader(inStr);
arr=readAll(buffR,length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(buffR != null){
try {
buffR.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(inStr != null){
try {
inStr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fil != null){
try {
fil.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return arr;
}
/**
* 读取文件中的所有字符,以int[]类型返回
* @param buffR
* @param length 文件长度,可以用File对象的length()方法得到
* @return
*/
private static int[] readAll(BufferedReader buffR,int length){
int index=;
int[] arr=new int[length];
while(index<length){
try {
arr[index]=buffR.read();
index++;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return arr;
}
/**
* 写入文件
* @param panth
* @param arr :需要写入字符的int类型编码
*/
private static void writeFile(String panth,int[] arr){
File dir=new File(panth);
/*
* 判断文件是否存在
*/
if(!dir.exists()){
try {
dir.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
FileWriter fil=null;
BufferedWriter buffW=null;
try {
fil=new FileWriter(dir);
buffW=new BufferedWriter(fil);
writeAll(buffW,arr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(buffW != null){
try {
buffW.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fil != null){
try {
fil.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 写入文件
* @param panth
* @param string :需要写入文件的字符串
*/
private static void writeFile(String panth,String string){
File dir=new File(panth);
/*
* 判断文件是否存在
*/
if(!dir.exists()){
try {
dir.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
FileWriter fil=null;
BufferedWriter buffW=null;
try {
fil=new FileWriter(dir);
buffW=new BufferedWriter(fil);
buffW.write(string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(buffW != null){
try {
buffW.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fil != null){
try {
fil.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*char[] arr=string.toCharArray();
for(char v:arr){
}*/
}
/**
* 全部写入文件
* @param buffW
* @param arr
*/
private static void writeAll(BufferedWriter buffW,int[] arr){
for(int i=;i<arr.length;i++){
try {
buffW.write(arr[i]);
if(arr[i] == )
buffW.write();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}