今天新建了个文本文件,内容为:你好!测试文档!然后通过如下代码读取(myeclipse6.0环境),一开始没有转码都是正常显示的,后来安装了myeclipse7.0
,在7.0下无论我怎么转码都显示乱码,请问怎么解决:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;public class Test {
public String readTxt(int off, int leng) {
RandomAccessFile r = null;
String str = "";
String str1 = "";
try {
r = new RandomAccessFile(new File("D:/aa.txt"), "r");
byte[] c = new byte[leng];
r.seek(off);
r.read(c);
str = new String(c);
str1 = new String(str.getBytes("iso8859-1"),"utf-8");
} catch (Exception e) {
try {
r.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return str1;
}
public static void main(String[] args) {
Test t = new Test();
String str = t.readTxt(0,5);
System.out.println(str);
}
}
,在7.0下无论我怎么转码都显示乱码,请问怎么解决:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;public class Test {
public String readTxt(int off, int leng) {
RandomAccessFile r = null;
String str = "";
String str1 = "";
try {
r = new RandomAccessFile(new File("D:/aa.txt"), "r");
byte[] c = new byte[leng];
r.seek(off);
r.read(c);
str = new String(c);
str1 = new String(str.getBytes("iso8859-1"),"utf-8");
} catch (Exception e) {
try {
r.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return str1;
}
public static void main(String[] args) {
Test t = new Test();
String str = t.readTxt(0,5);
System.out.println(str);
}
}
解决方案 »
- 如果坦克和子弹有相同的move方法怎么办
- Java?先学c!!!!!
- 算法问题啊~~~~~~~~关于排序,不知道有没有人会啊!哎
- 关于方法中对象的定义细节
- java TCP 编程,读取一个永不断开InputStream,怎样保证数据完整,不断开流,并且把数据处理好?
- 请教如何检验用户传入的类型是正确的?
- 如何使用JAVA编写分析IP数据包的程序?
- 读研的方向:C好还是JAVA好?(另现在开始学java,有没有比较好的书籍推荐一下?)
- Java能否做到把取得的有格式文字转化成流,再把这个流转化成有格式文字?如果可以,应该怎么做?
- IIS+Tomcat整合后,程序的部署问题。
- 关于String的问题...
- 问一个java反射机制的可见性问题
import java.io.IOException;
import java.io.RandomAccessFile;public class Test {
public String readTxt(int off, int leng) {
RandomAccessFile r = null;
String str = "";
//String str1 = "";
try {
r = new RandomAccessFile(new File("D:/aa.txt"), "r");
byte[] c = new byte[leng];
r.seek(off);
r.read(c);
str = new String(c);
//str1 = new String(str.getBytes("GB2312"), "utf-8");
} catch (Exception e) {
try {
r.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return str;
} public static void main(String[] args) {
Test t = new Test();
String str = t.readTxt(0, 5);
System.out.println(str);
}
}