是指这种? ctrl+z在命令行就是结束输入流的意思,但是必须换行单独使用 import java.util.Scanner;/* * 注意:必须先回车再ctrl+z结束输入,否则当前行不会保存 */ public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); StringBuffer sb = new StringBuffer(); while(in.hasNextLine()){ String str = in.nextLine(); sb.append("\n"); sb.append(str);
private void init(File inputFile,File outputFile) throws FileNotFoundException{ if(inputFile==null) this.is = System.in; else this.is = new FileInputStream(inputFile); if(outputFile==null) this.os = System.out; else this.os = new FileOutputStream(outputFile); } private void readAndWrite() throws IOException{ do{ int c = this.is.read(); //this way is only used temporary, it can be used to break the //process when the user pressing "Ctrl^Z"; if(c == -1)break; this.os.write(c); }while(true); }
private void print() throws IOException{ int c = -1; do{ c = this.is.read(); this.os.write(c); }while(c > 0);
ctrl+z在命令行就是结束输入流的意思,但是必须换行单独使用
import java.util.Scanner;/*
* 注意:必须先回车再ctrl+z结束输入,否则当前行不会保存
*/
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
while(in.hasNextLine()){
String str = in.nextLine();
sb.append("\n");
sb.append(str);
}
System.out.println(sb.toString());
//把sb写入文件....
}
}
import java.io.*;
import java.util.*;public class A { public static void main(String[] args) { System.out.println("请输入:");
try {
Scanner sc = new Scanner(System.in); File file = new File("E:/userinput.txt"); FileWriter fout = new FileWriter(file); String str; while (sc.hasNext()) {
str = sc.nextLine();
str = str + "\r\n";
fout.write(str);
}
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;public class ReadAndWrite {
private InputStream is ;
private OutputStream os;
private void init(File inputFile,File outputFile) throws FileNotFoundException{
if(inputFile==null)
this.is = System.in;
else
this.is = new FileInputStream(inputFile);
if(outputFile==null)
this.os = System.out;
else
this.os = new FileOutputStream(outputFile);
} private void readAndWrite() throws IOException{
do{
int c = this.is.read();
//this way is only used temporary, it can be used to break the
//process when the user pressing "Ctrl^Z";
if(c == -1)break;
this.os.write(c);
}while(true);
}
private void print() throws IOException{
int c = -1;
do{
c = this.is.read();
this.os.write(c);
}while(c > 0);
}
private void close(InputStream is,OutputStream os){
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static String fileSep = System.getProperty("file.separator");
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String fileName = "useInput.txt";
File f = new File("E:"+fileSep+"temp"+fileSep);
if(!f.exists()){
f.mkdirs();
}
f = new File(f.getAbsolutePath()+fileSep+fileName);
f.createNewFile();
// if(true)return;
ReadAndWrite raw = new ReadAndWrite();
try {
raw.init(null, f);
raw.readAndWrite();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
raw.close(raw.is, raw.os);
}
//print on Console
raw.init(f, null);
try{
raw.print();
}finally{
raw.close(raw.is, raw.os);
}
}}