读取本地的txt文件里的中文信息就不会乱码,如果读取MF(打成JAR包后解压出来的META-INF文件里的一个文件)格式的文件就会乱码!public static void readFileByLines(String fileName) {
File file = new File("./swatch/META-INF/MANIFEST.MF");
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println( tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
编译后的结果:
ine 1: Manifest-Version: 1.0
line 2: MIDlet-Vendor: 閽荤煶鎯呬汉1
line 3: MIDlet-Version: 1.0
line 4: MIDlet-Permissions-Opt: javax.microedition.io.Connector.https,javax.mi
line 5: croedition.io.Connector.file.read,javax.microedition.io.Connector.fil
line 6: e.write,javax.microedition.io.Connector.socket,javax.microedition.io.
line 7: Connector.sms,javax.wireless.messaging.sms.receive,javax.microedition
line 8: .media.control.VideoControl.getSnapshot,javax.microedition.content.Co
line 9: ntentHandler
line 10: MIDlet-Install-Notify: http://51xs8.com/install.jsp?yld=0
line 11: Created-By: 1.6.0_21 (Sun Microsystems Inc.)
line 12: MIDlet-Info-URL: http://wap.51xs8.com
line 13: MIDlet-Name: 閽荤煶鎯呬汉1
line 14: MIDlet-1: 閽荤煶鎯呬汉1, l.png, Main
line 15: MicroEdition-Configuration: CLDC-1.0
line 16: MIDlet-Icon: /l.png
line 17: MIDlet-Permissions: javax.microedition.io.Connector.http
line 18: MicroEdition-Profile: MIDP-2.0
File file = new File("./swatch/META-INF/MANIFEST.MF");
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println( tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
编译后的结果:
ine 1: Manifest-Version: 1.0
line 2: MIDlet-Vendor: 閽荤煶鎯呬汉1
line 3: MIDlet-Version: 1.0
line 4: MIDlet-Permissions-Opt: javax.microedition.io.Connector.https,javax.mi
line 5: croedition.io.Connector.file.read,javax.microedition.io.Connector.fil
line 6: e.write,javax.microedition.io.Connector.socket,javax.microedition.io.
line 7: Connector.sms,javax.wireless.messaging.sms.receive,javax.microedition
line 8: .media.control.VideoControl.getSnapshot,javax.microedition.content.Co
line 9: ntentHandler
line 10: MIDlet-Install-Notify: http://51xs8.com/install.jsp?yld=0
line 11: Created-By: 1.6.0_21 (Sun Microsystems Inc.)
line 12: MIDlet-Info-URL: http://wap.51xs8.com
line 13: MIDlet-Name: 閽荤煶鎯呬汉1
line 14: MIDlet-1: 閽荤煶鎯呬汉1, l.png, Main
line 15: MicroEdition-Configuration: CLDC-1.0
line 16: MIDlet-Icon: /l.png
line 17: MIDlet-Permissions: javax.microedition.io.Connector.http
line 18: MicroEdition-Profile: MIDP-2.0
解决方案 »
- 关于java多线程的一点疑问
- 第一次assignment,看得我想哭,有人能看看吗?
- java中怎样调用oracle中返回的varchar2的值啊!!!!
- 来拿分了~~我想做个1到1000之间偶数的总和,奇数的总和,不知道该怎么做了。高手赐教
- 关于fileChooser.showOpenDialog 打开后,我不选择文件,出现空指针异常
- 在自己建立的文件夹下建立文件,可怎么都不对头!!SOS!!
- 一个简单的程序,书上的,有问题.
- 奇怪的時間
- Java精英请进!!!
- 又一个关于类的的设计的问题?(UML?)
- java连oracle异常,请帮忙看下,谢谢!
- 初学者 public ()里边有什么不同的 代表什么意思?
用InputStreamReader,这个可以指定编码。