Reading and writing String files with JSON in java -


i want read text file , process it's data.so example input file :

john,judd,134 kaufman,kim,345 

then program should parse , store these data in form of json file organized further processing.i'm using json-simple task .and prototype code i've written:

package com.company;  import org.json.simple.jsonobject;  import java.io.*;  public class main {   static jsonobject jsonobject = new jsonobject(); static string output;  public static void main(string[] args) throws ioexception {      read("/users/sepehr/desktop/jsonexample.txt");     write("/users/sepehr/desktop/jsonexampleout,txt");  }  public static string read(string filenamein) throws ioexception {      bufferedreader bufferedreader = new bufferedreader(new filereader(filenamein));     string s ;      while ( (s = bufferedreader.readline() ) != null)      {         string[] stringsarr = s.split(",");           jsonobject.put( "famname" , stringsarr[0] );         jsonobject.put("name" , stringsarr[1]);         jsonobject.put("id", stringsarr[2]);          bufferedreader.close();       }      return output=jsonobject.tojsonstring();   }   public static string write(string filenameout) throws filenotfoundexception {      printwriter printwriter = new printwriter(filenameout);     printwriter.write(jsonobject.tojsonstring());     printwriter.close();      string se = "yaaayyy :|";     return se;  }    } 

after running program these exceptions get:

exception in thread "main" java.io.ioexception: stream closed @ java.io.bufferedreader.ensureopen(bufferedreader.java:97) @ java.io.bufferedreader.readline(bufferedreader.java:292) @ java.io.bufferedreader.readline(bufferedreader.java:362) @ com.company.main.read(main.java:30) @ com.company.main.main(main.java:18) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ com.intellij.rt.execution.application.appmain.main(appmain.java:140) 

what wrong ?

and how should make better design program ?

you closing bufferedreader in loop

while ( (s = bufferedreader.readline() ) != null)      {         string[] stringsarr = s.split(",");           jsonobject.put( "famname" , stringsarr[0] );         jsonobject.put("name" , stringsarr[1]);         jsonobject.put("id", stringsarr[2]);          //*************         bufferedreader.close(); // don't close reader!         //*************      } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -