java问题:创建一个顺序文件,向其中写入键盘输入的字符,并回显在屏幕上。

来源:百度知道 编辑:UC知道 时间:2024/05/28 16:12:30
这个程序怎么写

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;

public class Test {

private OutputStream outStream;
private File f;

public OutputStream getOStream(File f) {
try {
outStream = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件未找到");
return null;
}
return outStream;
}

public static void main(String[] args) {
Test t = new Test();
File f = new File("c:/a.txt");
OutputStream ostream=t.getOStream(f);
Scanner sc = new Scanner(System.in);
while (true) {

try {
String tmpString = sc.nextLine();
if (tmpString != null) {
System.out.println(tmpString);
os