从键盘读取一行数据,并输出至文件才c:\123.txt 若输入eof 且按回车 停止 文件被创建 然后程序停止运行
import java.io.*;
import java.lang.*;
public class keyboard3 {
public static void main(String [] arge)throws Exception{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
FileWriter fw = new FileWriter("c:\\123.txt");
BufferedWriter bw =new BufferedWriter(fw);
System.out.println("请输入内容(结束:eof):");
String data;
while(true)
{
data=br.readLine();
if(data.equals("eof"))break;
bw.write(data);
bw.newLine();
}
bw.close();
System.out.println("文件创建完毕");
}
}
这个是我做的 但是我没发现哪里错 程序就一直在执行 键入EOF也没反应
if (data.equalsIgnoreCase("eof"))//不分大小写
代码没有问题。 且正常写入到了文本.
看是不是你的 eof 输入的是大写.
所以,为了不分大小写~改为以上的方法
//if(data.equals("eof")||data.equals("EOF"))break; //IgnoreCase
if(data.compareToIgnoreCase("eof")==0);
//两种修改方法 个人认为第一个效率更高
必须是在新行开始才行如果是在一行中的eof是没法比较的 他一直为假 所以不能停止
有找到该方法的 给个提示哈我认为要解决那个问题只有自己写一个方法了
public boolean regionMatches(int toffset,String other,int ooffset,int len) //字符串包含函数
String str = "can you feel the love tonight";
String str2 = "love";
int i = str2.length();
for(int t=0;t<str.length()-i;t++){
if(str.regionMatches(0,str2,0,i)==true)
break;
} 我们都可以运行 为什么你的不行
版本不对?1.5试试
public static void main(String [] arge)throws Exception{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
FileWriter fw = new FileWriter("c:\\123.txt");
BufferedWriter bw =new BufferedWriter(fw);
System.out.println("请输入内容(结束:exit):");
String data=br.readLine();
while(!data.equalsIgnoreCase("exit"))
{
bw.write(data);
bw.newLine();
System.out.print(">>");
data=br.readLine();
}
bw.close();
fw.close();
br.close();
System.out.println("文件创建完毕");
}
}
我换了个字符竟然行了不知道啥问题 汗。。谢谢大家这么热情的帮我