这个java程序有什么问题
importjava.io.*;importjava.util.*;publicclassTestprintstream2{publicstaticvoidmain(St...
import java.io.*;
import java.util.*;
public class Testprintstream2{
public static void main(String[] args){
String s = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
FileWriter fw = new FileWriter("f:/java/filelog.log",true);
PrintWriter log = new PrintWriter(fw);
while((s=br.readLine())!=null){
if(s.equalsIgnoreCase("exit")) break;
System.out.println(s.toUpperCase());
log.println("------");
log.println(s.toUpperCase());
log.flush();
}
log.println("=="+new Date()+"==");
log.flush();
log.close();
}catch(IOException a){
System.out.println("文件错误");
}
}
} 展开
import java.util.*;
public class Testprintstream2{
public static void main(String[] args){
String s = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
FileWriter fw = new FileWriter("f:/java/filelog.log",true);
PrintWriter log = new PrintWriter(fw);
while((s=br.readLine())!=null){
if(s.equalsIgnoreCase("exit")) break;
System.out.println(s.toUpperCase());
log.println("------");
log.println(s.toUpperCase());
log.flush();
}
log.println("=="+new Date()+"==");
log.flush();
log.close();
}catch(IOException a){
System.out.println("文件错误");
}
}
} 展开
4个回答
展开全部
FileWriter fw = new FileWriter("f:/java/filelog.log",true); //这句有错,路径
应该是
FileWriter fw = new FileWriter("f://java//filelog.log",true); //另外,要存在f:\java目录
应该是
FileWriter fw = new FileWriter("f://java//filelog.log",true); //另外,要存在f:\java目录
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有问题啊,好着呢,就是从控制台读数据,然后写入到文件中去。如果读到的数据是exit,则退出循环,把日期写到日志中,并且刷新流,保存所有日志信息,关闭输出流。 我是在Windows平台下试的,没有问题。如果是Linux,文件路径分隔符改成双斜杠。
追问
为什么我在命令提示符里面输入 会显示文件错误
追答
E:\ex\online>java Testprintstream2
java.io.FileNotFoundException: f:\java\filelog.log (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileWriter.(Unknown Source)
at Testprintstream2.main(Testprintstream2.java:9)
?????í?ó
。。。。
控制台下这么整,输入的内容只能放在这个String[] args数组中。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
就不能这么用了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.*;
import java.util.*;
public class Testprintstream2{
public static void main(String[] args){
String s = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
FileWriter fw = new FileWriter("f:/java/filelog.log",true);
PrintWriter log = new PrintWriter(fw);
while((s=br.readLine())!=null){
if(s.equalsIgnoreCase("exit")) break;
System.out.println(s.toUpperCase());
log.println("------");
log.println(s.toUpperCase());
log.flush();
}
log.println("=="+new Date()+"==");
log.flush();
log.close();
}catch(IOException a){
//******Found****** 加上下面这句,看看出错路径。希望有帮助
a.printStackTrace();
System.out.println("文件错误");
}
}
}
E:\ex\online>java Testprintstream2
java.io.FileNotFoundException: f:\java\filelog.log (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at Testprintstream2.main(Testprintstream2.java:9)
?????í?ó
import java.util.*;
public class Testprintstream2{
public static void main(String[] args){
String s = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
FileWriter fw = new FileWriter("f:/java/filelog.log",true);
PrintWriter log = new PrintWriter(fw);
while((s=br.readLine())!=null){
if(s.equalsIgnoreCase("exit")) break;
System.out.println(s.toUpperCase());
log.println("------");
log.println(s.toUpperCase());
log.flush();
}
log.println("=="+new Date()+"==");
log.flush();
log.close();
}catch(IOException a){
//******Found****** 加上下面这句,看看出错路径。希望有帮助
a.printStackTrace();
System.out.println("文件错误");
}
}
}
E:\ex\online>java Testprintstream2
java.io.FileNotFoundException: f:\java\filelog.log (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at Testprintstream2.main(Testprintstream2.java:9)
?????í?ó
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询