一、代码如下
package www.wujianbotwo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Demo10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo10 demo10= new Demo10();
demo10.charRead();
}
public void charRead() {
try {
File file= new File("D:\Test.txt");
InputStream in= new FileInputStream(file);
InputStreamReader reader= new InputStreamReader(in, "GBk");
int temp;
while((temp=reader.read()) != -1) {
if((char)temp != '
') {
System.out.print((char)temp);
}
}
reader.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();
}
}
}
package www.wujianbotwo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Demo10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo10 demo10= new Demo10();
demo10.charRead();
}
public void charRead() {
try {
File file= new File("D:\Test.txt");
InputStream in= new FileInputStream(file);
InputStreamReader reader= new InputStreamReader(in, "GBk");
int temp;
do {
temp= reader.read();
if((char)temp != '
') {
System.out.print((char)temp);
}
}while(temp != -1);
//while((temp=reader.read()) != -1) {
// if((char)temp != '
') {
// System.out.print((char)temp);
// }
//}
reader.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();
}
}
}
public void charWrite() {
try {
File file= new File("D:\Test.txt");
OutputStream in= new FileOutputStream(file, true);
OutputStreamWriter out= new OutputStreamWriter(in, "GBK");
String conten= "我要回家。
";
char[] ch= conten.toCharArray();
out.write(ch, 0, ch.length);
out.flush();
out.close();
System.out.println("写入成功");
} 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();
}
}
public void charWriteTwo() {
try {
File file= new File("D:\Test.txt");
OutputStream in= new FileOutputStream(file, false);
OutputStreamWriter out= new OutputStreamWriter(in, "GBK");
String conten= "我要回家。
";
char[] ch= conten.toCharArray();
out.write(ch, 0, ch.length);
out.flush();
out.close();
System.out.println("写入成功");
} 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();
}
}
package www.wujianbotwo;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Demo11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo11 demo11= new Demo11();
demo11.bufferedRead();
}
public void bufferedRead() {
// TODO Auto-generated method stub
try {
File file= new File("D:\Test.txt");
InputStream in= new FileInputStream(file);
InputStreamReader reader= new InputStreamReader(in, "GBK");
BufferedReader br= new BufferedReader(reader);
String line= "";
do {
line= br.readLine();
System.out.println(line);
}while (line != null);
br.close();
reader.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();
}
}
}