package IOStream;
import java.io.BufferedInputStream;
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.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//读和写
public class DataProcess {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("C:\\Users\\Hasaky\\Desktop\\testWrite.txt");
}
//通过字符流读数据
static String readbychar01(File f) {
String result=null;
try {
FileReader fr=new FileReader(f);
char[] ch=new char[1024];
int len=0;
while((len=fr.read(ch))!=-1) {
result=new String(ch,0,len);
}
fr.close();
}catch (Exception e) {
// TODO: handle exception
}
return result;
}
//通过字符流写数据
static void writebychar01(File f,String content) {
try {
FileWriter fw=new FileWriter(f);
fw.write(content);
fw.flush();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//通过字符缓冲流读数据
static String readbychar02(File f) {
String result=null;
try {
BufferedReader br=new BufferedReader(new FileReader(f));
char[] ch=new char[1024];
int len=0;
while((len=br.read(ch))!=-1) {
result=new String(ch,0,len);
}
br.close();
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
//通过字符缓冲流写数据
static void writebychar02(File f,boolean Boolean,String content) {
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(f, Boolean));
bw.write(content);
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readbybyte02(File f) {
String result=null;
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
byte[] buff=new byte[1024];
int len=0;
while((len=bis.read(buff))!=-1){
result=(new String(buff,0,len));
}
bis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void writebybyte02(File F,boolean Boolean,String Content) {
//boolean判断是否是追写
try {
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(F,Boolean));
int len=Content.length();
for(int i=0;i<len;i++) {
bos.write((int)Content.charAt(i));
}
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}