Revised Question on File Processing

Discussion in 'Java' started by Subhabrata, Jan 27, 2013.

  1. Subhabrata

    Subhabrata Guest

    Dear Group,

    I was trying to read a file get the output in the form of words and
    trying to compare with the words generated from a sentence. I was
    trying to write the following code.

    It is giving me an output of the file in the form of words, but after
    that I am not being able to attach any more processings. It is
    throwing an exception as,
    Exception in thread "main" java.lang.NullPointerException
    at FileIntake1.main(FileIntake1.java:21)

    But if I write the code with try and catch, true exception is not
    there, but no output.
    And I am just new to Java my question is if you write try and catch a
    file, how one post processes a file within try? Isn't there a smart
    solution?
    And how would I go here?

    I am bit new to Java if you can kindly direct.

    Regards,
    Subhabrata.



    #1) Code No: 1

    import java.io.*;
    public class FileIntake1 {

    /**
    * @param args
    */
    public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("C:\\FileIO\\JAVADICT1.txt");
    BufferedReader br = new BufferedReader(fr);
    String s2="Moscow is the Capital of Russia";
    String [] s3=s2.split(" ");
    int l2=s3.length;

    String str;
    while ((str = br.readLine()) != null)
    //System.out.println("The Strings In the File are:");
    System.out.println(str);
    String [] s1=str.split(" ");
    int l1=s1.length;
    for(int x = 0; x < l1; x = x+1) {
    //System.out.print("value of x : " + x );
    System.out.println(s1[x]);



    }

    br.close();









    }
    }


    #2) Code No: 2
    import java.io.*;

    public class FileInput{

    public static void main(String args[]){

    try{
    byte bWrite [] = {11,21,3,40,5};
    OutputStream os = new FileOutputStream("C:\\FileIO\
    \JAVADICT1.txt");
    for(int x=0; x < bWrite.length ; x++){
    os.write( bWrite[x] ); // writes the bytes
    }
    os.close();

    InputStream is = new FileInputStream("C:\\FileIO\
    \JAVADICT1.txt");
    int size = is.available();

    for(int i=0; i< size; i++){
    System.out.print((char)is.read() + " ");
    }
    is.close();
    }catch(IOException e){
    System.out.print("Exception");
    }
    }
    }
    Subhabrata, Jan 27, 2013
    #1
    1. Advertising

  2. Subhabrata

    Guest

    On Sunday, January 27, 2013 12:47:16 PM UTC+5:30, Subhabrata wrote:
    > Dear Group,
    >
    >
    >
    > I was trying to read a file get the output in the form of words and
    >
    > trying to compare with the words generated from a sentence. I was
    >
    > trying to write the following code.
    >
    >
    >
    > It is giving me an output of the file in the form of words, but after
    >
    > that I am not being able to attach any more processings. It is
    >
    > throwing an exception as,
    >
    > Exception in thread "main" java.lang.NullPointerException
    >
    > at FileIntake1.main(FileIntake1.java:21)
    >
    >
    >
    > But if I write the code with try and catch, true exception is not
    >
    > there, but no output.
    >
    > And I am just new to Java my question is if you write try and catch a
    >
    > file, how one post processes a file within try? Isn't there a smart
    >
    > solution?
    >
    > And how would I go here?
    >
    >
    >
    > I am bit new to Java if you can kindly direct.
    >
    >
    >
    > Regards,
    >
    > Subhabrata.
    >
    >
    >
    >
    >
    >
    >
    > #1) Code No: 1
    >
    >
    >
    > import java.io.*;
    >
    > public class FileIntake1 {
    >
    >
    >
    > /**
    >
    > * @param args
    >
    > */
    >
    > public static void main(String[] args) throws IOException {
    >
    > FileReader fr = new FileReader("C:\\FileIO\\JAVADICT1.txt");
    >
    > BufferedReader br = new BufferedReader(fr);
    >
    > String s2="Moscow is the Capital of Russia";
    >
    > String [] s3=s2.split(" ");
    >
    > int l2=s3.length;
    >
    >
    >
    > String str;
    >
    > while ((str = br.readLine()) != null)
    >
    > //System.out.println("The Strings In the File are:");
    >
    > System.out.println(str);
    >
    > String [] s1=str.split(" ");
    >
    > int l1=s1.length;
    >
    > for(int x = 0; x < l1; x = x+1) {
    >
    > //System.out.print("value of x : " + x );
    >
    > System.out.println(s1[x]);
    >
    >
    >
    >
    >
    >
    >
    > }
    >
    >
    >
    > br.close();
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > }
    >
    > }
    >
    >
    >
    >
    >
    > #2) Code No: 2
    >
    > import java.io.*;
    >
    >
    >
    > public class FileInput{
    >
    >
    >
    > public static void main(String args[]){
    >
    >
    >
    > try{
    >
    > byte bWrite [] = {11,21,3,40,5};
    >
    > OutputStream os = new FileOutputStream("C:\\FileIO\
    >
    > \JAVADICT1.txt");
    >
    > for(int x=0; x < bWrite.length ; x++){
    >
    > os.write( bWrite[x] ); // writes the bytes
    >
    > }
    >
    > os.close();
    >
    >
    >
    > InputStream is = new FileInputStream("C:\\FileIO\
    >
    > \JAVADICT1.txt");
    >
    > int size = is.available();
    >
    >
    >
    > for(int i=0; i< size; i++){
    >
    > System.out.print((char)is.read() + " ");
    >
    > }
    >
    > is.close();
    >
    > }catch(IOException e){
    >
    > System.out.print("Exception");
    >
    > }
    >
    > }
    >
    > }


    Dear Group,
    Sorry to disturb you. I have fixed the issue. Regards,Subhabrata.
    , Jan 27, 2013
    #2
    1. Advertising

  3. Subhabrata

    Roedy Green Guest

    On Sat, 26 Jan 2013 23:17:16 -0800 (PST), Subhabrata
    <> wrote, quoted or indirectly quoted someone
    who said :

    >.NullPointerException

    see
    http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
    Roedy Green, Jan 28, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1.  opalinski from opalpaweb

    pair.class revised

    opalinski from opalpaweb, Mar 15, 2006, in forum: Java
    Replies:
    2
    Views:
    3,143
    Oliver Wong
    Mar 16, 2006
  2. Replies:
    1
    Views:
    585
    Webcastmaker
    Jul 19, 2004
  3. Spartanicus
    Replies:
    0
    Views:
    396
    Spartanicus
    Sep 16, 2004
  4. Scott Robert Ladd
    Replies:
    31
    Views:
    6,346
    Carsten Hansen
    Jan 7, 2004
  5. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    410
    Michael Foord
    Sep 17, 2004
Loading...

Share This Page