import java.io.*;
public class Zuoye{
public static void main(String args[])throws IOException{
String str = null;
PrintWriter out = new PrintWriter(new FileWriter("c:/tem1.txt"));
while(true){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
str = in.readLine();
out.println(str);
if(str.equals("end"))
break;
}
out.close();
}
}
public class Zuoye{
public static void main(String args[])throws IOException{
String str = null;
PrintWriter out = new PrintWriter(new FileWriter("c:/tem1.txt"));
while(true){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
str = in.readLine();
out.println(str);
if(str.equals("end"))
break;
}
out.close();
}
}
解决方案 »
- 为什么这段代码运行输出是这样的东西?
- UnsupportedEncodingException: GB2312 怎么解决
- 引入包的时候,import java.sql.*和分别引入有区别么?
- acegi
- 红字部分的用法不明白,请求帮助
- 如何用JAVA实现这个盒子?
- 请问SmartUpload的官方网站和源码在哪里能得到???
- 大家看看这个程序对不对?请教!!!!
- 关于java.util.regex正则表达式的使用
- Applet问题:如何在关闭Applet的同时,一起将Applet所在IE窗口关闭?
- util.Date类中的getYear(),getMonth()多Deprecated了,那我如何达到这两个函数的效果?
- catch 提示被另一catch块隐藏!
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output;
StringBuffer sb = new StringBuffer();
System.out.println("Program started...");
String str = input.readLine().trim();
while (!str.equalsIgnoreCase("end")) {
sb.append(str+"\r\n");
str = input.readLine().trim();
}
input.close();
output = new BufferedWriter(new FileWriter("c:\\teml.txt"));
output.write(sb.toString());
output.close();
System.out.println("Successfully saved to file. Program will exit.");
}
}
tem1.txt里!!可能是大家把题目理解错了!!
我在说明一下题目的意思:
从控制台里输入一些字符,当输入字符里有"end"的时候,回车程序结束,并且把前面输入的字符存到指定文件里,当输入字符没有"end"的时候,回车后,把字符存到指定文件里。还有那位高人肯帮助??
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
// 这句话把系统输入流包装成BufferedReader便于阅读用户输入
BufferedWriter output; // 输出流到文件
System.out.println("Program started...");
String str = input.readLine().trim();
// 读取输入存到String对象中,trim用于去掉无用空格。
String end = str.substring(str.length()-3);
// 取得字符串后面3个字符组成的子串
if (end.equalsIgnoreCase("end")) { // 判定是否以end结尾
output = new BufferedWriter(new FileWriter("c:\\teml.txt"));
// 打开或新建文件c:\teml.txt,用BufferedWriter封装输出流
output.write(str.substring(0, str.length()-3));
// 将前面的字符输出
output.close(); // 关闭输出流
System.out.println("Successfully saved to file. Program will exit.");
}
else {
System.out.println("Input not ended with 'end', program will exit.");
}
}
}
这里能够找到所有用到的类的说明文档。
abcendxyz这样的字符串输入以后需要做什么相应操作?
public class Zuoye{
public static void main(String args[])throws IOException{
String str = null;
PrintWriter out = new PrintWriter(new FileWriter("c:/tem1.txt"));
while(true){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
str = in.readLine();
if(str.lastIndexOf("end")!=-1){
if(str.lastIndexOf("end")!=0)
out.println(str.substring(0,str.lastIndexOf("end")));
break;
}else{
out.println(str);
}
}
out.close();
}
}
还有你们有联络方式没有?我希望你们带带我!!!
sean_gao(大胃) ( ) 你的“output = new BufferedWriter(new FileWriter("c:\\teml.txt"));”好象错了!应该是c:/tem1.txt吧
“str.substring(0,str.lastIndexOf("end")));”是什么意思?