java吧 关注:1,304,504贴子:12,868,360
  • 9回复贴,共1

哪里错了怎么改

只看楼主收藏回复

import java.io.*;
public class fileStream
{
static FileInputStream fis;
static FileOutputStream fos; //声明静态文件输入、出流
static int a; //定义静态变量a
public static void main(String arg[])
{
try
{
copyFile(); //调用复制文件函数
fis.close();
fos.close(); //关闭流
}
catch(FileNotFoundException fe) //捕捉异常
{
System.out.println("文件无法创建!");
}
catch(IOException ie)
{
System.out.println("输入有误!");
}
}
static void copyFile() throws IOException,FileNotFoundException //文件复制函数,并抛出异常
{
fis=new FileInputStream("D:/a/1.txt");
fos=new FileOutputStream("D:/a/2.txt"); //创建文件输入、出流,构造函数参数为从哪个文本到哪个文本
while((a=fis.read())!=-1)
fos.write(a); //通过循环调用read()函数从流中读取一个字节,传递给变量a,并判断是否读完(读完函数返回值-1),未读完,则通过输出流的write()函数,将字节写出到流中
System.out.println("复制完毕,请查看!");
}
}


1楼2012-04-27 22:24回复
    怎么都是复制


    2楼2012-04-27 22:26
    回复
      2026-06-16 02:07:22
      广告
      不感兴趣
      开通SVIP免广告
      我这儿运行正常啊


      IP属地:浙江3楼2012-04-27 22:28
      回复
        这程序本来不就是复制吗


        IP属地:浙江4楼2012-04-27 22:28
        回复
          是啊 我想把文件里的内容复制出来


          5楼2012-04-27 22:41
          回复
            怎么改 或者怎么导入


            6楼2012-04-27 22:41
            回复
              出来就只有复制没有内容


              7楼2012-04-27 22:41
              回复
                你再定义个String 每次复制都+=不就好


                IP属地:浙江9楼2012-04-27 22:44
                回复
                  2026-06-16 02:01:22
                  广告
                  不感兴趣
                  开通SVIP免广告
                  看一下


                  10楼2012-04-27 22:45
                  回复
                    package com.aptech.io;
                    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;
                    public class FileIO {
                    public void FileDemo() throws IOException { // java IO的核心对象
                    File f = new File("d:/abc.rmvb"); if (!f.exists()) { // 创建文件 f.createNewFile(); }
                    File f1 = new File("d:/def"); if (!f1.exists()) { // 创建文件夹 f1.mkdir(); } // 文件对象是否是目录 System.out.println(f1.isDirectory()); // 文件对象是否是文件 System.out.println(f.isFile()); // 获得文件夹下的所有文件对象 // f1.listFiles() }
                    public void readFileByByte() throws IOException { // Stream 字节流 InputStream is = new FileInputStream("d:\\abc.txt"); // 设置读写的缓冲大小 byte[] b = new byte[1024];
                    int len = -1; // 将流读入到字节数组中 while ((len = is.read(b)) != -1) { // String(字节数组,起始下标,读字节的长度) // String str=new String(b,0,len,"GBK"); String str = new String(b, 0, len); System.out.println(str); } is.close(); }
                    public void writeFileByByte() throws IOException { // Stream 字节流 // FileOutputStream(文件对象,是否追加) OutputStream os = new FileOutputStream("d:\\abc.txt", true); String str = "123123213\r\n324324324\r\n23432"; // 写字节数组 os.write(str.getBytes()); os.close(); }
                    /** * 复制文件 * * @throws IOException * 作者:贺刚 Date:2011-7-29 */ public void copyFileByByte(String fromFile, String toPath) throws IOException { // 获得来源文件对象 File file = new File(fromFile); // 获得来源文件输入流 InputStream is = new FileInputStream(file); // 获得目标文件对象 String toFile = toPath + "/" + file.getName(); // 获得目标文件输出流 OutputStream os = new FileOutputStream(toFile); // 10k的缓冲 byte[] b = new byte[10240]; int len = -1; while ((len = is.read(b)) != -1) { os.write(b, 0, len); } os.close(); is.close();
                    }
                    // Reader writer public void readFileByChar() throws IOException { // FileReader fr=new FileReader("d:/abc.txt"); // char[] ch=new char[1024]; // int len=-1; // while((len=fr.read(ch))!=-1){ // String str=new String(ch,0,len); // System.out.println(str); // }
                    InputStreamReader isr = new InputStreamReader(new FileInputStream( "d:/abc.txt"), "GBK"); char[] ch = new char[1024]; int len = -1; while ((len = isr.read(ch)) != -1) { String str = new String(ch, 0, len); System.out.println(str); } }
                    public void copyDirectory( String fromPath, String toPath) throws IOException { File from = new File(fromPath); // 判断是否是文件夹 if (from.isDirectory()) { //创建目标文件夹 String toFilePath=toPath + "/" + from.getName(); File f=new File(toFilePath); f.mkdir(); //获得子文件对象 File[] files = from.listFiles(); if (files != null) { for (File file : files) { //是否是文件夹 if(file.isDirectory()){ //递归调用复制文件夹 copyDirectory(file.getPath(),f.getPath()); //删除目录 }else if(file.isFile()){ //是否是文件 copyFileByByte(file.getPath(),f.getPath()); //删除文件 } } }
                    }
                    }
                    public static void main(String[] args) throws IOException { FileIO f = new FileIO(); // f.FileDemo(); // f.readFileByByte(); // f.writeFileByByte(); // f.copyFileByByte("d:\\soft\\myeclipse-8.0.0-win32.exe", "d:/def");// f.readFileByChar(); f.copyDirectory("d:/def", "e:/test"); }}
                    希望能帮到你 231386375 java技术交流群 期待加入


                    IP属地:湖南11楼2012-04-27 22:45
                    回复