62,623
社区成员
发帖
与我相关
我的任务
分享
package test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class TestForIn {
public static void main(String[] args) {
try {
Scanner scanner=new Scanner(System.in);
PrintStream systemOut=System.out;
System.setOut(new PrintStream(new FileOutputStream("aa.txt")));
String strTemp;
while (scanner.hasNextLine()) {
strTemp=scanner.nextLine();
if (strTemp.endsWith("#")) {
System.out.println(strTemp.substring(0, strTemp.length()-1));
break;
}
System.out.println(strTemp);
}
scanner.close();
System.setOut(systemOut);
System.out.println("程序结束!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
//创建输入流接收键盘的输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//创建输出流
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("c:/c.txt")))) ;
//读取信息
String s = null;
List<String> contents = new ArrayList<String>();
while((s=reader.readLine())!=null){
if(!s.equals("#")){
contents.add(s+"\n");
}else{
break;
}
}
for(String content : contents){
writer.write(content);
}
writer.flush();
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}