在网上找到了如下代码,经过简单的修改想把读的文件写到output中,编译后没有错误,执行后常产生了output文件,但里面是空的,没有写入。
还有就是infile = new FileInputStream(args[0]);是什么意思阿?
不知道是什么原因,请各位帮帮忙!
import java.io.*;
public class Readfile
{
public static void main(String[] args)
{
byte[] buff = new byte[1024];
boolean cont = true;
FileInputStream infile = null;
try
{
infile = new FileInputStream(args[0]); /** ??? */
}
catch (FileNotFoundException e)
{
System.err.println("error");
System.exit(1);
}
while (cont)
{
try
{
int n = infile.read(buff);
FileOutputStream outfile = new FileOutputStream("output.txt");//!!!
outfile.write(buff, 0, n);
}
catch (Exception e)
{
cont = false;
}
}
try
{
infile.close();
}
catch (IOException e)
{
System.err.println("error");
System.exit(1);
}
}
}
还有就是infile = new FileInputStream(args[0]);是什么意思阿?
不知道是什么原因,请各位帮帮忙!
import java.io.*;
public class Readfile
{
public static void main(String[] args)
{
byte[] buff = new byte[1024];
boolean cont = true;
FileInputStream infile = null;
try
{
infile = new FileInputStream(args[0]); /** ??? */
}
catch (FileNotFoundException e)
{
System.err.println("error");
System.exit(1);
}
while (cont)
{
try
{
int n = infile.read(buff);
FileOutputStream outfile = new FileOutputStream("output.txt");//!!!
outfile.write(buff, 0, n);
}
catch (Exception e)
{
cont = false;
}
}
try
{
infile.close();
}
catch (IOException e)
{
System.err.println("error");
System.exit(1);
}
}
}
你可以用
java Readfile.java
这样,就可以,把Readfile.java中的内容,复制到output.txt中了!
java Readfile input.txt
把input.txt的内容复制到output.txt中。但问题就是执行后output.txt 中是空的,根本就没复制上。