• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 794
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
Jalpa KotakCommented:
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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