微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 尝试创建文本文件操纵器时,文本未附加到新文件中

问题和我所处的位置:我无法将文本附加到我使用该程序创建的这些新文件中.目前它只复制文件但不附加它们.见评论
“//将文件名附加到新文件中”.

其次,最终转储文件似乎只附加.java文件,它不读取或附加输入文件.

我正在尝试做的解释:

长和短是我试图制作一个程序,将其放入随机文件夹中,其中.txt文件填充数据.

我需要该程序

>只查看它所在文件夹的范围
>然后拿任何.txt文件
a)制作副本,但将原始文件名附加到文本正文(在顶部),在“< filenamehere.txt”之类的子文件夹中添加到正文中(在顶部) b)然后复制original.txt的正文内容
c)获取前置的.txt文件并将其附加到单个dump.txt文件
d)对所有本地txt文件重复此操作,并继续追加到dump.txt文件的末尾

所以最后,如果我有7个文件,我将有7个附加副本和1个巨大转储文件,其中包含7个附加副本的所有内容.例如,如果我有三个文本文件,每个文件只有一个单词.所以a.txt,b.txt,c.txt
这三个词是“Hello world!”. a.txt副本将包含内容

“> A.TXT

你好

现在它只是复制Hello(原始正文内容)但不附加> a.txt.最终的转储文本文件不会累积其他文件中的任何内容,但奇怪的是从.java文件获取代码.基本上,我得到一个// Output文件夹,里面是.txt文件的副本和megaDump.txt,它们设法获取.java文本,但没有附加其他文本文件.

import java.io.* ;
import java.nio.file.*;

public class FilePrepender // class name 
{
    public static void main (String [] args)
    {
        // make a giant dump file which we will append all read files into  
        try {
            new File("Output\\").mkdirs();
            File megaDumpFile = new File("Output\\masterDump.txt");

            if (megaDumpFile.createNewFile()) {
                System.out.println("File creation success");
            } else {
                System.out.println("File was not made. File already exists. Please delete");
            }

        } catch (IOException e) {

        }

        //grab file names 
        File folder = new File(".");
        File[] listofFiles = folder.listFiles();
        for (int i = 0; i < listofFiles.length; i++) {
            if (listofFiles[i].isFile()) {
                listofFiles[i].getName();
            } else if (listofFiles[i].isDirectory()) {
                //do nothing
            }
        }

        //open files + duplicate + prepend + and append product to end of master dump file 
        // main for 
        for (int j = 0; j < listofFiles.length; j++){
            //append file name for mega dump file 
            String fileNameTemp = listofFiles[j].getName();  // get file name 
            try { 
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Output//masterDump.txt",true)));
                out.println(fileNameTemp);
                out.flush();
                out.close();
            }
            catch (IOException e) {  

            }

            // duplicate input files 
            FileInputStream instream = null;
            FileOutputStream outstream = null;

            try {
                File infile =new File(""+listofFiles[j].getName());
                File outfile =new File("Output\\"+listofFiles[j].getName());

                instream = new FileInputStream(infile);
                outstream = new FileOutputStream(outfile);

                byte[] buffer = new byte[1024];

                int length;

                // apend file name into the new file 
                // String fileNameTemp = listofFiles[j].getName();  // get file name 

                try { 
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Output//masterDump.txt",true)));
                    out.println(">"+fileNameTemp);
                    out.flush();
                    out.close();
                }
                catch (IOException e) {  

                }


                // Now copy contents of prevIoUs file into the new file 

                /*copying the contents from input stream to
                * output stream using read and write methods
                */
                while ((length = instream.read(buffer)) > 0){
                    outstream.write(buffer,length);
                }
                //Closing the input/output file streams
                instream.close();
                outstream.close();

                // file is copied 
            } catch(IOException ioe) { 

            }

            // copy newly copied file into mega dump 
            try {
                File infile =new File("Output\\"+listofFiles[j]); // newly copied 
                File outfile =new File("Output\\masterDump.txt");

                instream = new FileInputStream(infile);
                outstream = new FileOutputStream(outfile);

                byte[] buffer = new byte[1024];

                int length;
                /*copying the contents from input stream to
                * output stream using read and write methods
                */
                while ((length = instream.read(buffer)) > 0){
                    outstream.write(buffer,length);
                }

                //Closing the input/output file streams
                instream.close();
                outstream.close();

                // file is copied 

            } catch(IOException ioe) {

            }

        } // end for loop 
    } // end main 
} // end class 
最佳答案
这里有很多问题:

>您使用的文件路径有时会斜线,有时2个反斜杠,有时甚至是双斜线,这至少在我的Mac上导致问题.只需使用常规正斜杠即可.
>代码还没有过滤.txt文件,因此处理了当前目录中的所有内容 – 甚至是正在执行的程序本身.
>目前代码写了> sometext.txt直接进入masterDump.txt,而不是通过文件副本间接进行.
>代码覆盖了循环的每次迭代的masterDump.txt,因为文件未在附加模式下打开.

以下是当在包含“Hello”,“World”和“!”的a.txt,b.txt和c.txt的文件夹中调用时,当前产生以下结果的代码.分别.我希望这是理想的行为.
请注意,此代码中有很多需要改进的地方,尤其是处理注释中已经指出的错误.

Result

import java.io.* ;
import java.nio.file.*;

public class FilePrepender // class name 
{
    public static void main (String [] args)
    {
        // make a giant dump file which we will append all read files into  
        try {
            new File("Output/").mkdirs();
            File megaDumpFile = new File("Output/masterDump.txt");

            if (megaDumpFile.createNewFile()) {
                System.out.println("File creation success");
            } else {
                System.out.println("File was not made. File already exists. Please delete");
            }

        } catch (IOException e) {

        }

        //grab file names 
        File folder = new File(".");
        File[] listofFiles = folder.listFiles();
        for (int i = 0; i < listofFiles.length; i++) {
            if (listofFiles[i].isFile()) {
                listofFiles[i].getName();
            } else if (listofFiles[i].isDirectory()) {
                //do nothing
            }
        }

        //open files + duplicate + prepend + and append product to end of master dump file 
        // main for 
        for (int j = 0; j < listofFiles.length; j++){
            //append file name for mega dump file 
            String fileNameTemp = listofFiles[j].getName();  // get file name
            if (!fileNameTemp.toLowerCase().endsWith(".txt")) {
                continue;
            } 

            // duplicate input files 
            FileInputStream instream = null;
            FileOutputStream outstream = null;

            try {
                File infile =new File(""+listofFiles[j].getName());
                File outfile =new File("Output/"+listofFiles[j].getName());

                instream = new FileInputStream(infile);


                byte[] buffer = new byte[1024];

                int length;

                // apend file name into the new file 
                // String fileNameTemp = listofFiles[j].getName();  // get file name 
                outstream = new FileOutputStream(outfile);
                PrintWriter out = new PrintWriter(outstream);
                out.println(">"+fileNameTemp);
                out.flush();
                out.close();

                // Now copy contents of prevIoUs file into the new file 

                /*copying the contents from input stream to
                * output stream using read and write methods
                */
                outstream = new FileOutputStream(outfile,true);
                while ((length = instream.read(buffer)) > 0){
                    outstream.write(buffer,length);
                }
                //Closing the input/output file streams
                instream.close();
                outstream.close();

                // file is copied 
            } catch(IOException ioe) { 

            }

            // copy newly copied file into mega dump 
            try {
                File infile =new File("Output/"+listofFiles[j]); // newly copied 
                File outfile =new File("Output/masterDump.txt");

                instream = new FileInputStream(infile);
                outstream = new FileOutputStream(outfile,true);

                byte[] buffer = new byte[1024];

                int length;
                /*copying the contents from input stream to
                * output stream using read and write methods
                */
                while ((length = instream.read(buffer)) > 0){
                    outstream.write(buffer,length);
                }

                //Closing the input/output file streams
                instream.close();
                outstream.close();

                // file is copied 

            } catch(IOException ioe) {

            }

        } // end for loop 
    } // end main 
} // end class 

原文地址:https://www.jb51.cc/java/437320.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐