先读到一个byte数组里面,然后用String的构造器构造,第一个参数为此数组,第二个参数为字符编码
QQ1818477欢迎交流
QQ1818477欢迎交流
解决方案 »
- 使用GUI控件自动生成的jTextArea在类外为什么不能对其操作?
- 各位老大帮我看下这个菜鸟问题,关于读取文件的
- 待线程高人指点一二,程序有点问题!
- 在论坛上看到这么一个题目,有装满药片的5个广口瓶,其中一个瓶子的药片已经被污染。没污染的药每片10gm,被污染的每片9pm。给你一个天平,如何只经过一次测量找出被污染的药瓶?
- 如何进行文本筛选
- 关于在JSP页面里的悬浮广告的问题
- 如何读入中文文件,总是乱码?
- 我很想学JAVA2,可是我不知道该怎么安装它?用什么安装?
- JBuilder使用时,光标和打出的程序好像有点错位,这是怎么回事
- 对ERP项目有兴趣的同志们,请进!
- java菜問題
- 今天开始学习JAVA,望各位高手指教一二
import java.io.*;public class ReadFile {
public static void main (String[] args) {
// Create file
if (args.length!=1){
System.out.println("Usage: java ReadFile filename");
return ;
}
File file = new File(args[0]);
StringBuffer sb = new StringBuffer (); try {
// Create a buffered reader to read each line from a file.
BufferedReader in = new BufferedReader(new FileReader(file));
// Read each char and chain.
int i;
while ( (i=in.read())!= -1 ) {
sb.append((char)i);
}
// Close the buffered reader, which also closes the file reader.
in.close(); } catch (FileNotFoundException e1) {
// If this file does not exist
System.err.println("File not found: " + file);
} catch (IOException e2) {
// Catch any other IO exceptions.
e2.printStackTrace();
}
System.out.println(" content of '"+args[0]+"'");
System.out.println(sb.toString()); }
}
FileInputStream fis=new FileInputStream(filepath);
byte[] b=new byte[fis.available()];
fis.read(b);
String myString=new String(b);
try {
// Create a buffered reader to read each line from a file.
BufferedReader in = new BufferedReader(new FileReader(file));
// Read each char and chain.
String temp;
while ( (temp=in.readLine())!= null ) {
sb.append(temp);
}
// Close the buffered reader, which also closes the file reader.
in.close();