簡體   English   中英

將程序的輸出寫入文件

[英]writing the output of a program into a file

我編寫了一個程序,將pdf解析為文本。 我正在控制台中獲取輸出,但無法將其寫入文件。 這是我完成的代碼:

public class PDFTextParser {

public static void main(String args[]) throws IOException {
    PDFTextStripper pdfStripper = null;
    COSDocument cosDoc = null;
    try {


         File file = new File("1.pdf");
         PDDocument pdDoc = PDDocument.load(file);
         pdfStripper = new PDFTextStripper();
         String parsedText = pdfStripper.getText(pdDoc);
         System.out.println(parsedText);
         FileWriter out = new FileWriter("output.txt"); 
         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
         String line = in.readLine();
         while (line!= null) {

                 out.append(line);
                 out.append("\n");
               }
        out.close();
    }catch (IOException e) {
         e.printStackTrace();}
   }
}

輸出為:

Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser      parseFileObject
WARNING: Object (6:0) at offset 1013093 does not end with 'endobj' but  with '7'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (7:0) at offset 1013211 does not end with 'endobj' but with '483'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (9:0) at offset 1020280 does not end with 'endobj' but with '10'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (10:0) at offset 1020396 does not end with 'endobj' but with '15'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (15:0) at offset 1020519 does not end with 'endobj' but with '16'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (16:0) at offset 1020640 does not end with 'endobj' but with '17'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (17:0) at offset 1020756 does not end with 'endobj' but with '18'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (18:0) at offset 1020874 does not end with 'endobj' but with '19'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (19:0) at offset 1020993 does not end with 'endobj' but with '24'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (24:0) at offset 1021111 does not end with 'endobj' but with '25'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (25:0) at offset 1021228 does not end with 'endobj' but with '26'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (26:0) at offset 1021350 does not end with 'endobj' but with '27'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (27:0) at offset 1021469 does not end with 'endobj' but with '28'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (28:0) at offset 1021589 does not end with 'endobj' but with '489'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (458:0) at offset 1026684 does not end with 'endobj' but with '463'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (463:0) at offset 1026809 does not end with 'endobj' but with '464'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (464:0) at offset 1026932 does not end with 'endobj' but with '465'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (465:0) at offset 1027050 does not end with 'endobj' but with '466'
Apr 07, 2016 2:04:10 PM org.apache.pdfbox.pdfparser.COSParser parseFileObject
WARNING: Object (466:0) at offset 1027170 does not end with 'endobj' but with '495'

並且已解析的pdf文本出現在控制台中..但是我得到一個空文件作為輸出

您已經從PDF中獲取了文本,只需將其寫入文件,其余的代碼會嘗試從不需要的用戶(例如鍵盤)獲取輸入,只需使用以下代碼:

String parsedText = pdfStripper.getText(pdDoc);
System.out.println(parsedText);
FileWriter out = new FileWriter("output.txt"); 
out.append(parsedText);
out.close();

//no need for this code, it reads input from user (using keyboard)
 /*
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 String line = in.readLine();
 while (line!= null) {

         out.append(line);
         out.append("\n");
       }
out.close();
*/

您是否查看過此帖子? 系統輸出到Java中的文件

但是我喜歡他的第一個答案

java -jar myjar.jar > output.txt

在你的情況下會像

java -cp <classpath>/PDFTextParser > output.txt

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM