Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用PrintWriter创建文件,并写入该文件_Java_File_File Io_Stream_Printwriter - Fatal编程技术网

在Java中使用PrintWriter创建文件,并写入该文件

在Java中使用PrintWriter创建文件,并写入该文件,java,file,file-io,stream,printwriter,Java,File,File Io,Stream,Printwriter,我试图编写一个程序来读取一个文件(这是一个Java源文件),从该文件中生成一个特定值的Arraylist。并将该Arraylist输出到另一个结果文件中 我正在使用PrintWriter生成新的结果文件。这是我的程序的摘要版本: ArrayList<String> exampleArrayList = new ArrayList<String>(); File actualInputFile = new File("C:/Desktop/example.java");

我试图编写一个程序来读取一个文件(这是一个Java源文件),从该文件中生成一个特定值的Arraylist。并将该Arraylist输出到另一个结果文件中

我正在使用PrintWriter生成新的结果文件。这是我的程序的摘要版本:

 ArrayList<String> exampleArrayList = new ArrayList<String>();
 File actualInputFile = new File("C:/Desktop/example.java");

 PrintWriter resultingSpreadsheet= new PrintWriter("C:/Desktop/SpreadsheetValues.txt", "UTF-8");

 FileReader fr = new FileReader(actualInputFile);
 BufferedReader br = new BufferedReader(fr);
 String line=null;

 while ((line = br.readLine()) != null) {
 // code that makes ArrayList
 }

 for (int i = 0; i < exampleArrayList.size(); i++) {
      resultingSpreadsheet.println(exampleArrayList.get(i));
 }
 resultingSpreadsheet.close();
这是我想要作为输入文件的文件,大小为481KB, 与:

它实际上只是.java源文件的一个较小的.txt示例版本,大小为1.08KB

我尝试了一些方法,包括刷新PrintWriter,将其包装在FileWriter周围,将.java文件中的所有代码复制粘贴到文本文件中,以防出现扩展问题,但这些方法似乎不起作用。 我开始认为这一定是因为PrintWriter生成的文件的大小,但这很可能不是问题所在。也许我需要把所有的东西都放在一条流中(就像这里说的:)?如果是的话,我会怎么做

为什么在SmallerInput文件一切正常的情况下,读取更大的RealizationInputFile并正确输出其数据会出现这样的问题?
有人能帮忙吗

这应该作为注释,但我没有代表。在文档中,它既有写入方法,也有打印方法。您尝试过使用write()吗

我怀疑这是文件的大小,它可能在您正在测试的两个文件之间,一个是.txt,另一个是.java


编辑:可能是这两个建议中的第二个。首先是我在文档中注意到的一些东西。

这应该作为一个注释,但我没有代表。在文档中,它有写入方法和打印方法。您尝试过使用write()吗

我怀疑这是文件的大小,它可能在您正在测试的两个文件之间,一个是.txt,另一个是.java


编辑:可能是这两个建议中的第二个。首先是我注意到的文档。

PrintWriter的方法不会抛出异常。调用
checkError()
方法,如果发生错误,该方法将刷新流并返回true。处理较大文件时很可能发生错误,例如编码错误。

PrintWriter的方法不会引发异常。调用
checkError()
方法,如果发生错误,该方法将刷新流并返回true。处理较大的文件时很可能发生错误,例如编码错误。

在写入excel工作表时检查异常,因为我真的认为这不是大小问题。下面是成功执行的示例代码,文件大小约为1MB

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("D:\\AdminController.java"));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

}

在写入excel工作表时检查异常,因为我真的不认为这是大小问题。下面是成功执行的示例代码,文件大小约为1MB

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("D:\\AdminController.java"));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

}

检查你的程序。当文件为空时,表示程序在完成程序之前没有关闭PrintWriter。
例如,在程序的某个部分中可能有一个返回值,该返回值会导致resultingSpreadsheet.close();没有被运行

检查您的程序。当文件为空时,表示程序在完成程序之前没有关闭PrintWriter。
例如,在程序的某个部分中可能有一个返回值,该返回值会导致resultingSpreadsheet.close();没有被运行

您还没有展示如何处理异常。您对java文件有什么看法吗?您还没有展示如何处理异常。你对java文件有什么看法吗?
public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("D:\\AdminController.java"));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

}