为什么我的代码卡住了,无法完成运行?

时间:2014-12-22 03:41:37

标签: logging console

我一直试图在下面记录我的程序,但我似乎无法在控制台上显示。它只跑到某个部分然后卡住了。一直试图追溯每一行,但我无法弄清楚原因。

以下是代码:

package tutorial;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.logging.*;
import java.util.HashMap;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Element;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

//import com.itextpdf.text.Font;

public class watermark {

    // Set LOGGING
    private static Logger logger = Logger.getLogger(watermark.class.getName());

    public static void main(String[] args) {

        // Set LOGGING HANDLER
        Handler[] handlers = Logger.getLogger( "" ).getHandlers();
        for (int index = 0; index < handlers.length; index++) {
            handlers[index].setLevel(Level.FINE);
        }

        logger.setLevel(Level.FINE);
        // Start program
        long startTime = System.currentTimeMillis();

        try {
            BufferedReader br = new BufferedReader(new FileReader("D:\\Documents\\java\\listcos"));
            String line;

            Scanner in1 = new Scanner(System.in);
            System.out.println("What's the open password?");
            String user = in1.nextLine();
            byte[] USER = user.getBytes();

            Scanner in2  = new Scanner(System.in);
            System.out.println("What's the secure password?");
            String owner = in2.nextLine();
            byte[] OWNER = owner.getBytes();

            while(user != owner) {

                try{

                    while ((line = br.readLine()) != null) {

                        try {

                            String a = line;
                            PdfReader Post_Survey = new PdfReader("sample.pdf");
                            int number_of_pages = Post_Survey.getNumberOfPages();
                            PdfStamper stamp = new PdfStamper(Post_Survey, new FileOutputStream("Sample" + line + ".pdf"));
                            // set Security
                            stamp.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);


                            // Update Metadata
                            HashMap<String, String> hMap = Post_Survey.getInfo();
                            hMap.put("Title", "Post Survey Presentation");
                            hMap.put("Subject", "Market Highlights");
                            hMap.put("Creator", "Zestiremia");
                            hMap.put("Keywords","                                 \n            * * * * *            \n          *             *          \n        *    * * * *    *        \n       *         *         *       \n       *       *           *       \n       *     * * * *     *       \n        *                 *        \n          *             *          \n      * * * *       * * * *      \n                                 ");
                            hMap.put("Author", "SSC - Talentnet");
                            stamp.setMoreInfo(hMap);

                            int i = 0;

                            Image watermark_image = Image.getInstance("watermark.png");
                            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);

                            watermark_image.setAbsolutePosition(20, 40);
                            PdfContentByte add_watermark;
                            PdfContentByte add_text;

                            //start stamping!!
                            while (i < number_of_pages) {
                              i++;


                              // watermark image
                              add_watermark = stamp.getOverContent(i);
                              add_watermark.addImage(watermark_image);

                              // set where lines are      
                              add_text = stamp.getOverContent(i);
                              add_text.setFontAndSize(bf, 12);
                              ColumnText.showTextAligned(add_text,Element.ALIGN_LEFT, new Phrase("This is the properties of blah blah and is solely used for " + line + " only!"), 20, 20, 0);

                              System.out.println("Partly done!");

                            }
                            stamp.close();
                            Post_Survey.close();

                        }catch (Exception e) {
                            e.printStackTrace();}
                    }
                }catch (Exception e) {
                    e.printStackTrace();}
            }   
        }catch (Exception e) {
            e.printStackTrace();}
        System.out.println("Done");
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println(totalTime/1000 + "s");
    }

    private String theMessage;

    public watermark(String message) {
        theMessage = message;
    }

    public void sayHello() {
        logger.fine("Hello logging!");
        System.err.println(theMessage);
    }

}

程序将一直运行到System.out.println("Partly done!")然后卡住。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我发现我有一个不应该存在的无限while(user!=owner)循环。

我把它改回if(user!=owner),一切顺利。