一道Java命令行的题目

angel8808 2010-05-23 02:16:55
提示用户开始输入,并将此后的键盘输入内容保存至文件userinput.txt中,直到用户在键盘上输入^z(control键+z键)结束输入,显示userinput.txt全部内容介绍程序运行。关键是^z结束输出怎么实现???
...全文
222 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼上的解决的很彻底啊,顶一下
  • 打赏
  • 举报
回复
Runtime.getRuntime().addShutdownHook
zsw0860 2010-05-23
  • 打赏
  • 举报
回复
四楼朋友用是javaBean思想很不错!但视乎跟楼主的要求有点相悖了
zhuyouyong 2010-05-23
  • 打赏
  • 举报
回复
顶[Quote=引用 4 楼 xiong_hh 的回复:]
Scanner 貌似无法扫描一些特殊的组合键,如果要求不是很严格的话可以考虑以下代码:

Java code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.……
[/Quote]
xiong_hh 2010-05-23
  • 打赏
  • 举报
回复
Scanner 貌似无法扫描一些特殊的组合键,如果要求不是很严格的话可以考虑以下代码:

import java.io.File;
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);
}

}

}

piaolankeke 2010-05-23
  • 打赏
  • 举报
回复
确实要单独起行才可以直接读取流判断字符
Adebayor 2010-05-23
  • 打赏
  • 举报
回复

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();
}
}
}

lacus87 2010-05-23
  • 打赏
  • 举报
回复
是指这种?
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写入文件....
}
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧