package test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class demo1 {
public static void main(String[] args) throws IOException
{
File file=new File("test1.txt");
file.createNewFile();
FileOutputStream fos = new FileOutputStream("e:/test1.txt",true);
FileInputStream fis =new FileInputStream("test1.txt");
ByteArrayOutputStream bos=new ByteArrayOutputStream();
fos.write("你个2货".getBytes());
int len=-1;
byte[] buff=new byte[6];
while((len=fis.read(buff))!=-1)
{
bos.write(buff,0,len);
}
String msg=new String(bos.toByteArray());
System.out.println(msg);
bos.close();
}
}
这样是复制吗?我怎么看都不像的样子,麻烦修改下, 还有剪切...file.delete(); 也麻烦顺便发下 ,谢谢了
------解决方案--------------------
- Java code
package test;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class demo1 {public static void main(String[] args) throws IOException{File file=new File("test1.txt"); file.createNewFile(); //创建要复制后的文件FileOutputStream fos = new FileOutputStream("e:/test1.txt",true); //要复制的文件读取流对象(字节流)FileInputStream fis =new FileInputStream("test1.txt"); //复制后的文件写入流对象(字节流)ByteArrayOutputStream bos=new ByteArrayOutputStream(); //字节数组fos.write("你个2货".getBytes()); //写入数据"你个二货",因为是字节流,所以要将字符串转换为字节数组//int len=-1;byte[] buff=new byte[6]; //申明一个长度为6的字节数组,用于读取流的缓冲区//改为int len = fis.read(buff);while((len=fis.read(buff))>0) //循环读取{bos.write(buff,0,len); //将读取出来的字节写入文件}String msg=new String(bos.toByteArray()); System.out.println(msg); //打印输出的读取数据fis.close(); //关闭读取流bos.close(); //关闭输出流}}
------解决方案--------------------
package AboutIO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputStreamTest {
public static void main(String[] args) throws IOException{
FileOutputStream fos=null;
FileInputStream fis=null;
try {
fis=new FileInputStream("G:/软工实验报告.rar");
fos=new FileOutputStream("G:/test.rar");
int hasRead;
byte[] buff=new byte[1024];
while((hasRead=fis.read(buff))>0){
fos.write(buff,0,hasRead);
}
System.out.print("over");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
fis.close();
fos.close();
}
}
}
不用创建个ByteArrayOutputStream bos=new ByteArrayOutputStream();啊
文件在硬盘中都是以二进制存的,所以用fis读进来,再用fos写进去不就是复制了(读的数组可以用byte,如果知道只是字符的话,可以用char,性能会高些)
剪切就是上面兄台说的,复制完了 删除不就得了。。。