FileInputStrem要求参数必须是文件
直接用
InputStream in=System.in
直接用
InputStream in=System.in
解决方案 »
- java怎么把文件删除进回收站而不是直接删除?
- 编译通过.但运行时出现NoSuchMethodError.高手帮查错
- 请NB的人帮忙说清楚为什么.
- RE: JScrollPane滚动条位置问题?
- 数据库连接失败
- SWING 画出的直线能否加事件?
- 简单的数组问题
- java 使用registry-3.1.3读取注册表字符串中文的乱码问题? 真心求助!!!急求!!正确的,分全给!
- 我在IE启动下启动WORD,怎么样控制WORD的初始页面格式为A4或者A3呢??
- JCreator Pro 和JCreator LE有何不同?
- 我一次要从一个文件里面读3万条数据,非常慢,有没有什么读取方法可以改善?
- java用命令行编译时怎么不对
改为InputStream in=System.in
但是data = fin.readchar();出错了
直接用
InputStream in=System.in
DataInputStream fin=new DataInputStream(in);data = fin.readchar();
改成
data = fin.readChar();
现在显示以下出错信息:
FileIODemo.java:85:unreported exception java.io.FileNotFoundException:must be caught or declared to be thrown
new FileOutputStream( new File("input.txt"));
^
FileIODemo.java:115:unreported exception java.io.IOException:must be caught or declared to be thrown
nKeyIn = fin.readChar();
^FileIODemo.java:125:variable data might not have been ininized fout.write(data);
^
FileIODemo.java:125:unreported exception java.io.IOException:must be caught or declared to be thrown
fout.write(data);
^
FileIODemo.java:141:unreported exception java.io.IOException:must be caught or declared to be thrown
fout.close();
^
^^
try{
...
}
catch(IOException e){
...
}
结构把他们包起来
new InputStreamReader(System.in));
String temp = "";
while((temp=in.readLine())!=null)
{
System.out.println(temp);
}
import java.io.*;
class FileIODemo
{
public static void main(String args[])
{
FileIODemo aFileIODemo = new FileIODemo();
}
public FileIODemo()
{
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
String temp = "";
DataInputStream fin=new DataInputStream(in);//出错 FileOutputStream fout =new FileOutputStream( new File("input.txt"));
char data;
try
{
while((temp=in.readLine())!=null)
{
System.out.println(temp);
}
for(int n=0;n<10;n++)
{
nKeyIn = fin.read();
fout.write(data);
}
}
catch (IOException e) {}
fout.close();
}
}
出错信息:C:\javacode>javac FileIODemo.java
FileIODemo.java:95: cannot resolve symbol
symbol : constructor DataInputStream (java.io.BufferedReader)
location: class java.io.DataInputStream
DataInputStream fin=new DataInputStream(in);
^
1 error
就不要了吧!
import java.io.*;
class FileIODemo
{
public static void main(String args[])
{
FileIODemo aFileIODemo = new FileIODemo();
}
public FileIODemo()
{
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
String temp = "";
// DataInputStream fin=new DataInputStream(in);//不要这句 FileOutputStream fout =new FileOutputStream( new File("input.txt"));//出错(unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown)
char data;
try
{
while((temp=in.readLine())!=null)
{
System.out.println(temp);
}
for(int n=0;n<10;n++)
{
nKeyIn = in.read(); fout.write(data);//出错 (variable data might not have been initialized) }
}
catch (IOException e) {} fout.close();//出错(unreported exception java.io.IOException; must be caught or
declared to be thrown) }
}
至于"variable data might not have been intialized",是应为在试用data之前未被正确初始化(赋值).
改后整理一下应该没错
//FileIODemo.java
import java.io.*;
class FileIODemo
{
public static void main(String args[])
{
FileIODemo aFileIODemo = new FileIODemo();
}
public FileIODemo()
{
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
String temp = "";
try
{ FileOutputStream fout =new FileOutputStream( new File("input.txt"));
char data;//这里该如何初始化data?? while((temp=in.readLine())!=null)
{
System.out.println(temp);
}
for(int n=0;n<10;n++)
{
nKeyIn = in.read(); fout.write(data);//出错 (variable data might not have been initialized) }
fout.close();
}
catch (IOException e) {}
}
}
C:\javacode>javac FileIODemo.java
FileIODemo.java:212: variable data might not have been initialized
fout.write(data);
^
1 error