lundi 29 juin 2015

Get the first column of a file and save it into a File

I want to extract the first column in a file using the delimiter "," and save it into a new File. Output generates this exception :

Exception in thread "main" java.lang.NullPointerException
    at Extract.main(Extract.java:26)

Here is the code that I used butI am not sure if it is correct or not:

public class Extract {

    public Extract()
    {
    }

     public static void main(String[] args) {

            BufferedReader in = null;
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter("/home/omar/Téléchargements/nursery.tmp"));


                in = new BufferedReader(new FileReader("pima.txt"));
                String read = null;
                while ((read = in.readLine()) != null) {
                    read = in.readLine();
                    String[] splited = read.split(",");
                    if (splited.length > 0)
                    {
                        out.append(splited[0].toString());
                        out.newLine();
                    }

                }

            } catch (IOException e) {
                System.out.println("There was a problem: " + e);
                e.printStackTrace();
            } finally {
                try {
                    in.close();

                } catch (Exception e) {
                }
            }
            File f = new File("prima.txt");
            f.delete();

            File f2 = new File("pima.tmp");
            f2.renameTo(new File("pima.txt"));

     }



}

Aucun commentaire:

Enregistrer un commentaire