文件读取的简单问题(在线等)

Lemon63609535 2006-04-04 05:10:08
我有个一汉字内容的文件,读出来之后都是乱码.请各位给予指点.
谢谢!!!
...全文
165 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hemiao_1993 2006-04-04
  • 打赏
  • 举报
回复
import java.io.*;

public class IOStreamDemo {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1. Reading input by lines:
BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();
// System.out.println(s2);

//1b. Reading standard input:
/* BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.print(stdin.readLine());*/

// 2.Input from memory
/* StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.print((char)c);*/

//3. Formatted memory input
/* try {
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.print((char)in3.readByte());
} catch(EOFException e) {
System.err.println("End of stream");
}*/

//4. File output
try {
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount ++ + ": " + s);
out1.close();
} catch(EOFException e) {
System.err.println("End of stream");
}
//4b. Read the "IODemo.out"
BufferedReader in1 = new BufferedReader(new FileReader("IODemo.out"));
while((s = in1.readLine()) != null)
System.out.println(s);
in.close();


//5. Storing & recovering data
/* DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeUTF("That was pi");
out2.writeDouble(1.41413);
out2.writeUTF("Square root of 2");
out2.close();
DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());*/

// 6. Reading/writing random access files
/* RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
for(int i = 0; i < 10; i++)
rf.writeDouble(i * 1.414);
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for(int i = 0; i < 10; i++)
System.out.println("Value " + i + ": " + rf.readDouble());
rf.close();*/
}


}
Lemon63609535 2006-04-04
  • 打赏
  • 举报
回复
谢谢楼上。呵呵,成功了。Unicode。:)
hemiao_1993 2006-04-04
  • 打赏
  • 举报
回复
乱码的唯一原因就是读和写所用的编码不同. 所以在读之前首先你要知道当初写这个文件时所用的编码.
即然是可视化的汉字文件, 有可能是用Unicode写的, 你用Reader读取一下试试看.
不行把代码发出来看看.
jobs002 2006-04-04
  • 打赏
  • 举报
回复
帖下代码,让高手给看看.........

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧