• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 796
  • Last Modified:

Read Text File and write a Text File

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");
    

}





}

Open in new window

0
Element_T
Asked:
Element_T
2 Solutions
 
ioantonCommented:
You should initialize the 'reader' variable from the line:

InputStreamReader reader = null;

as follows:

InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName));
0
 
ioantonCommented:
The same applies to the line:

OutputStreamWriter writer = null;

It should be replaced by:

OutputStreamWriter writer = new OutpuStreamWriter(new FileOutputStream(fileName));
0
 
Element_TAuthor Commented:
hmm..I tried your code but get flowing error code during compiling:

Compiling 3 source files to /Users/hasanturhal/NetBeansProjects/DateiEinlesen/build/classes
/Users/hasanturhal/NetBeansProjects/DateiEinlesen/src/LiesDateiEin.java:47: cannot find symbol
symbol  : class OutpuStreamWriter
location: class LiesDateiEin
           OutputStreamWriter writer = new OutpuStreamWriter(new FileOutputStream(fileName,true));
1 error
/Users/hasanturhal/NetBeansProjects/DateiEinlesen/nbproject/build-impl.xml:413: The following error occurred while executing this line:
/Users/hasanturhal/NetBeansProjects/DateiEinlesen/nbproject/build-impl.xml:199: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ioantonCommented:
Sorry, there is a type in error (missing a letter):

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileName,true));
0
 
Element_TAuthor Commented:
I try that too, but the same error...maybe something is wrong somewhere else...

But no errors during compiling, errors during running..

I think something is wrong in "start" class.

0
 
Element_TAuthor Commented:
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 LiesDateiEin.writeTextFile(LiesDateiEin.java:50)
        at Start.main(Start.java:14)
BUILD SUCCESSFUL (total time: 1 second)
0
 
Element_TAuthor Commented:
public class Start {

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

}





}
0
 
nordtorpCommented:
Line 60, do you have those dotted lines in your code?

If so, you should remove them.
0
 
ioantonCommented:
Make sure the file a.rtf does exist at the specified location.
0
 
J KCommented:
Relpacing
 InputStreamReader reader = null;
with
InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName));
and Replacing
OutputStreamWriter writer =null;
with
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileName));
works fine.....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now