java问题 输入输出

wanyoulin 2008-07-03 10:23:01
程序代码如下(读取文件):
import java.io.*;
public class wan3{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
FileInputStream du=new FileInputStream (f);
int c=du.read();
while (c!=-1){
System.out.print((char)c);
c=(char)du.read();
}
du.close();
}
}
能读取文件的内容,可除了文件的内容还读出一些“?”,请高手赐教。
...全文
124 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
baeeq0122 2008-07-04
  • 打赏
  • 举报
回复
呵呵 忘了改个地方,我没运行所以~~
import java.io.*;
public class Test1{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
Reader du=new FileReader(f); ///////忘记修改处
BufferedReader br = new BufferedReader(du);//进行封装是为了得到readLine()方法
String c=br.readLine();
while (c!=null){
System.out.print(c);
c=br.readLine();
}
br.close();
du.close();
}
}
  • 打赏
  • 举报
回复
BufferedReader



nhy520 2008-07-04
  • 打赏
  • 举报
回复
多看看java.io包.
wanyoulin 2008-07-04
  • 打赏
  • 举报
回复
非常感谢!!!!好厉害啊 呵呵呵
wanyoulin 2008-07-03
  • 打赏
  • 举报
回复
谢谢!!!
不过好像还是不对
baeeq0122 2008-07-03
  • 打赏
  • 举报
回复
不好意思~
网络有问题~提交了N次 不知道帖成没成功
补充
修改后是
import java.io.*;
public class wan3{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
FileInputStream du=new FileInputStream (f);
BufferedReader br = new BufferedReader(du);//进行封装是为了得到readLine()方法
String c=br.readLine();
while (c!=null){
System.out.print(c);
c=br.readLine();
}
br.close();
du.close();
}
}
yslan 2008-07-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 baeeq0122 的回复:]
你这里FileInputStream对象du 的方法read()是用来读取字节的~要想读取不出乱码 修改如下
import java.io.*;
public class wan3{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
FileInputStream du=new FileInputStream (f);
String c=du.readLine();
while (c!=null){
System.out.print(c);
c=du.readLine();
}
du.close();
}
}
[/Quote]
baeeq0122 2008-07-03
  • 打赏
  • 举报
回复
你这里FileInputStream对象du 的方法read()是用来读取字节的~要想读取不出乱码 修改如下
import java.io.*;
public class wan3{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
FileInputStream du=new FileInputStream (f);
String c=du.readLine();
while (c!=null){
System.out.print(c);
c=du.readLine();
}
du.close();
}
}
baeeq0122 2008-07-03
  • 打赏
  • 举报
回复
你这里FileInputStream对象du 的方法read()是用来读取字节的~要想读取不出乱码 修改如下
import java.io.*;
public class wan3{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
FileInputStream du=new FileInputStream (f);
String c=du.readLine();
while (c!=null){
System.out.print(c);
c=du.readLine();
}
du.close();
}
}
baeeq0122 2008-07-03
  • 打赏
  • 举报
回复
你这里FileInputStream对象du 的方法read()是用来读取字节的~要想读取不出乱码 修改如下
import java.io.*;
public class wan3{
public static void main (String args[])throws IOException
{
File f=new File ("D:\\wan.txt");
FileInputStream du=new FileInputStream (f);
String c=du.readLine();
while (c!=null){
System.out.print(c);
c=du.readLine();
}
du.close();
}
}

62,614

社区成员

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

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