troubleshooting Question

Read Text File and write a Text File

Avatar of Element_T
Element_TFlag for Germany asked on
Java
10 Comments2 Solutions812 ViewsLast Modified:
How I can read a text(rtf) file and write one in Java.

Here my solution, its from a book , little bit modified, but not working,

thanks

I get flowing code error:
run:
java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:61)
        at java.io.BufferedReader.<init>(BufferedReader.java:76)
        at java.io.BufferedReader.<init>(BufferedReader.java:91)
        at LiesDateiEin.readFile(LiesDateiEin.java:19)
        at Start.main(Start.java:13)
java.lang.NullPointerException
        at java.io.Writer.<init>(Writer.java:71)
        at java.io.BufferedWriter.<init>(BufferedWriter.java:83)
        at java.io.BufferedWriter.<init>(BufferedWriter.java:70)
        at LiesDateiEin.writeTextFile(LiesDateiEin.java:49)
        at Start.main(Start.java:14)
BUILD SUCCESSFUL (total time: 0 seconds)

import java.io.*;
import java.util.*;

public class LiesDateiEin {

    /**
     * Lädt eine Textdatei mit der angegebenen Zeichenkodierung
     * @param fileName Name der Datei
     * @param charSet Name der zeichenkodierung
     * @return String Objekt mit eingelesenem Text oder null bei Fehler
     */
    public static String readFile(String fileName) {
        String result = null;

        try {
            InputStreamReader reader = null;

            BufferedReader in = new BufferedReader(reader);
            StringBuilder buffer = new StringBuilder();

            int c;

            while ((c = in.read()) > 0) {
                buffer.append((char) c);
            }
            in.close();
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            result = null;
        }
        return result;
    }

    /*
     * Schreibt einen String als Textdatei in der angegebenen Zeichekodierung
     * @param date Zu schreibender String
     * @param charSet Zeichenkodierung 
     * return true bei Erfolg
     */
    public static boolean writeTextFile(String data, String fileName) {
        boolean result = true;

        try {

            OutputStreamWriter writer = null;

            BufferedWriter out = new BufferedWriter(writer);
            out.write(data,0, data.length());
            out.close();
        }catch (Exception e){
            e.printStackTrace();
            result = false;
        }
        return result; 
    }
}


------------------------------------


public class Start {

public static void main (String []args){
    String text = LiesDateiEin.readFile(".\\a.rtf");
    boolean status = LiesDateiEin.writeTextFile(text, ".\\ab.rtf");
    

}





}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros