import java.io.*;public class TestIO
{
public static void main(String [] args){
InputStreamReader r = new InputStreamReader(System.in);
OutputStreamWriter w = new OutputStreamWriter(System.out);
char[] buf = new char[100];
int n = 0; try{
while((n = r.read(buf)) > 0){
System.out.println(new String(buf,0,n));
w.write(buf,0,n);
}
}catch(IOException e){
System.out.println("error " + e);
System.exit(1);
}
}
}
你的程序没有问题呀,你把输出的语句给注释了干吗?
{
public static void main(String [] args){
InputStreamReader r = new InputStreamReader(System.in);
OutputStreamWriter w = new OutputStreamWriter(System.out);
char[] buf = new char[100];
int n = 0; try{
while((n = r.read(buf)) > 0){
System.out.println(new String(buf,0,n));
w.write(buf,0,n);
}
}catch(IOException e){
System.out.println("error " + e);
System.exit(1);
}
}
}
你的程序没有问题呀,你把输出的语句给注释了干吗?
我还提供了一输入生成新文件的程序:
import java.io.*;public class CreateFile{
public static void main(String[] args){
String strFilename;
DataInputStream stdIn = new DataInputStream(new BufferedInputStream(System.in));
try{
if (args.length == 0){
System.out.print("Please Input the File Name:");
strFilename = stdIn.readLine();
}
else{
strFilename = args[0];
}
DataOutputStream NewFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(strFilename)));
int strTemp;
System.out.println("Please input the context,End with \'#\'");
strTemp = stdIn.read();
while(strTemp != '#'){
NewFile.write(strTemp);
strTemp = stdIn.read();
}
System.out.println(NewFile);
NewFile.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
{
public static void main(String [] args){
InputStreamReader r = new InputStreamReader(System.in);
OutputStreamWriter w = new OutputStreamWriter(System.out);
char[] buf = new char[100];
int n = 0; try{
while((n = r.read(buf)) > 0){
w.write(buf,0,n);
w.flush();
}
}catch(IOException e){
System.out.println("error " + e);
System.exit(1);
}
}
}
while((n = r.read(buf)) > 0){
w.write(buf,0,n);
w.flush();
}