请教各位大侠,怎么读取D:\test.txt文件内容,并打印到控制台.
读取ini文件与txt文件相同吗?
读取ini文件与txt文件相同吗?
解决方案 »
- 从数据库读取数据,多线程发送,可是数据会重复。
- 不用ffmpeg实现获取视频缩略图
- 怎样用图形界面AWT,SWING设计计算器?
- 新手提问~!~~急。。
- Oracle中BLOB字段(TIF)格式转JPG,JAVA怎么实现转化?
- 那位好心人给个jtable的例子?要能自由增删列行的,谢谢
- 一道题目
- JBuilder7生成的MessageDriveBean的問題
- 刚刚学习java有些不懂的问题请教大家
- int[] 的hashcode方法的内部实现是怎么样的?
- 各位朋友讲几句,什么时候使用接口interface什么时候使用抽象类abstract?
- java菜鸟问题急急急!!!!!一段《21天精通java2》里的简单代码
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
in.close();
System.out.println(s2);
2. 应该是一样的, 可以试试.
FileReader fr = null;
try {
fr = new FileReader("d:\\test.txt");
br = new BufferedReader(fr);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = br.readLine()) != null) {
sb.append(str + "\n");
}
System.out.println(sb.toString());
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
}
FileReader f = new FileReader("c:\\MYP.dat");
BufferedReader br = new BufferedReader(f);
String str;
while((str=br.readLine())!=null) {
System.out.print(str);
}
br.close();
f.close();
redline()是整行读取,如果只读取字符,可以用FileReader的read()方法。
可是我执行下面程序时为什么会报错呢?
public class B
{
BufferedReader in = new BufferedReader(new FileReader( "D:\\test.txt "));
String s,s2 = new String();
while((s = in.readLine()) != null)
s2 += s+"\n";
in.close();
public static void main(String args[])
{
B b = new B();
System.out.println(s2);
}
}
{
public String readfile() throws IOException{
BufferedReader in = new BufferedReader(new FileReader( "D:\\test.txt "));
String s,s2 = new String();
while((s = in.readLine()) != null)
s2 += s+"\n";
in.close();
return s2;
}
public static void main(String args[])
{
B b = new B();
try {
System.out.println(b.readfile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class read
{
public static void main(String[] args)
{
int i;
try
{
FileInputStream in = new FileInputStream("D:\\test.txt");
while((i=in.read())!=-1)
{
System.out.print((char)i);
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
不过如果文件中有汉字的话会是乱码