请高手帮我看下这个程序,是把一个文件的每个字节全部变成字节0,用ultraedit打开看多出了很多个字节0,程序运行时间很长,小虾不胜感激!!package test1;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author Administrator
*/
public class Test1 { /**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int i = 0;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("c:\\Test1.dat");
while (fis.read() != -1) {
i++;
}
System.out.println(i + " " );
} catch (FileNotFoundException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e) {
} finally { try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
}
try
{
fos = new FileOutputStream("c:\\Test1.dat");
for(int j = 0;j < i;i++){
fos.write(0);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}catch(IOException e){
System.out.println(e.getMessage());
}finally { try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
}
} }
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author Administrator
*/
public class Test1 { /**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int i = 0;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("c:\\Test1.dat");
while (fis.read() != -1) {
i++;
}
System.out.println(i + " " );
} catch (FileNotFoundException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e) {
} finally { try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
}
try
{
fos = new FileOutputStream("c:\\Test1.dat");
for(int j = 0;j < i;i++){
fos.write(0);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}catch(IOException e){
System.out.println(e.getMessage());
}finally { try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
}
} }
for(int j = 0;j < i;i++){
fos.write(0);
}