Java io流详解三

时间:2023-03-09 17:02:36
Java io流详解三
public  class  IOpractise  {

public  void    iotest()  {
int b= 0;
FileInputStream fis = null;
try {
fis = new FileInputStream("C:\\Users\\wb-cjz286752\\Desktop\\小程序.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("系统找不到指定文件!");
System.exit(-1); } long num =0;
try {
while((b=fis.read())!=-1){
//System.out.println((char)b);
System.out.println(b);
num++;
}
fis.close();
System.out.println();
System.out.println("总共读了"+num + "个字节的文件");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误!");
} } public void iotest1() throws IOException{ try {
FileInputStream fis = new FileInputStream("C:\\Users\\wb-cjz286752\\Desktop\\小程序.txt");
int read = 0;
while((read=fis.read())!=-1){
System.out.println(read);
}
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public String iotest2() throws IOException{
StringBuffer result = new StringBuffer(); try {
BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\webelement.txt"));
String str = null;
while ((str=bf.readLine())!=null){
result.append(str);
//System.out.println(result);
}
bf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result.toString();
} public void iotest3(String filename) throws IOException{
StringBuffer result = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String str = null;
while((str=br.readLine())!=null){
result.append(System.lineSeparator()+str);
//System.out.println();
}
b r.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result.toString()); } public void iotest4() throws IOException{
try {
FileWriter fw = new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt"); fw.write("123llove");
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt");
int it =0;
while((it=fr.read())!=-1){
System.out.print((char)it); }
fr.close(); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void iotest5(){
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\webelement.txt");
int it =0;
char[] buf = new char[10];
StringBuilder sb = new StringBuilder();
try {
while((it=fr.read(buf))!=-1){
sb.append(new String(buf,0,it));
}
String str = sb.toString();
System.out.println(str);
fr.close(); } catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} } public void iotest6() throws IOException{
FileWriter fw = new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt",true);
try {
fw.write("1234567890testtest!!!!!!!");
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void iotest7(){
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\1111111111111.txt");
FileWriter fw = new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\2222222222222.txt");
char [] buf = new char[2];
int it =0;
while ((it=fr.read(buf))!=-1){
String str = new String(buf,0,it);
fw.write(str);
}
fw.flush();
fw.close();
fr.close(); } catch (IOException e) {
e.printStackTrace();
} } public void iotest8() throws IOException{
try {
FileReader fr = new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\2222222222222.txt");
BufferedReader br = new BufferedReader(fr);
String str = null;
StringBuilder sb = new StringBuilder();
while ((str=br.readLine())!=null){
sb.append(str);
}
br.close();
System.out.println(sb.toString()); } catch (FileNotFoundException e) {
e.printStackTrace();
}
} public void iotest9() throws IOException{
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\wb-cjz286752\\Desktop\\2222222222222.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\wb-cjz286752\\Desktop\\3333333333333.txt"));
String str = null;
while ((str=br.readLine())!=null){
bw.write(str);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} public void iotest10() throws IOException{
try {
FileInputStream fis = new FileInputStream("C:\\Users\\wb-cjz286752\\Desktop\\3333333333333.txt");
FileOutputStream fos = new FileOutputStream("C:\\Users\\wb-cjz286752\\Desktop\\4444444444444.txt");
int it = 0;
byte[] byt = new byte[1024];
while ((it=fis.read(byt))!=-1){
fos.write(byt,0 ,it);
}
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} } public static void main(String[] args) throws IOException {
IOpractise iop = new IOpractise();
//iop.iotest3("C:\\Users\\wb-cjz286752\\Desktop\\webelement.txt");
iop.iotest10();
} }