将数据附加到同一文件

时间:2018-04-13 06:21:50

标签: java file append

将数据附加到同一个文件时遇到了问题,尽管已经尝试但是收到了错误。

这是我的代码

    public static void main(String args[]) throws IOException {
            URLShortener u = new URLShortener(100, "https://is.gd/");

            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(new 
File("C:\\data.txt"))));
                    BufferedWriter bw = new BufferedWriter(new 
FileWriter("C:\\dataoutput.txt")))

                    {
                String line;
                while ((line = br.readLine()) != null) {
                    String shortenedUrl = u.shortenURL(line);
                    System.out.println(new String(
                            "URL:" + line + "\t" + shortenedUrl + "\tExpanded: " + u.expandURL(shortenedUrl)));
                    bw.write(shortenedUrl + "\r\n");

                    System.out.println("Appending new data to shortUrls");
                    try (FileWriter fw = new FileWriter("C:\\dataoutput.txt", true);
                            BufferedWriter bappend = new BufferedWriter(fw);
                            PrintWriter pw = new PrintWriter(bappend))
                    {
                        pw.println(shortenedUrl + "\r\n");

                    } catch(IOException e) {

                        e.getMessage();
                    }    

                }
            }
    }

这是错误。我的输出文件指向dataoutput文件,它仍然无法追加它。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 10, length 0
        at java.base/java.lang.String.checkBoundsBeginEnd(Unknown Source)
        at java.base/java.lang.String.substring(Unknown Source)
        at com.misc.tests.URLShortener.sanitizeURL(URLShortener.java:101)
        at com.misc.tests.URLShortener.shortenURL(URLShortener.java:67
        at com.misc.tests.URLShortener.main(URLShortener.java:151

1 个答案:

答案 0 :(得分:1)

我自己解决了 - 这就是答案。我在做的是 - 在第7行,我创建了'bw'指向文件'dataoutput.txt',在第19行我创建'fw'指向同一个文件,文件在7打开。所以,我已经改变了它相反,这个工作正常 - 我能够将数据附加到文件。

{
    private static final String INPUT_FILE =
            "C:\\PowerShell Automation\\data.txt";
    private static final String OUTPUT_FILE = 
            "C:\\PowerShell Automation\\dataoutput.txt";

    public static void main( String args[] ) throws IOException {
        URLShortener u = new URLShortener( 100, "https://is.gd/" );

        System.out.println( "Appending new data to shortUrls" );

        try( BufferedReader br = new BufferedReader(
                new InputStreamReader( new FileInputStream( new File(
                        INPUT_FILE ) ) ) );
            // *** open for append
            PrintWriter pw = new PrintWriter( new FileWriter(
                    OUTPUT_FILE, true ) );
            )
        {
            String line;
            while( ( line = br.readLine() ) != null ) {
                String shortenedUrl = u.shortenURL( line );

                System.out.println( new String(
                        "URL:" + line + "\t" + shortenedUrl + 
                        "\tExpanded: " + u.expandURL( shortenedUrl ) ) );

                pw.println( shortenedUrl );
            }
        }
    }