java - how to add data from text file and Sort this data data received from text file using fileInputStream -
i want should read text text file using file inputstream..and store data using set or method..and sort data using arraylist...
class songdetail used set values of song,moviename , artist.... there text file "text1.txt" had store song/moviename/artist details... want when read file in class1,string reurned splited song,moviename,artist , set in songdetail class.
import java.io.bufferedreader; import java.io.fileinputstream; import java.io.inputstreamreader; import java.util.*; public class class1 { public static void main(string[] args) { //arraylist <string>list = new arraylist<string>(); arraylist <string>list = new arraylist<string>(); try { fileinputstream in = new fileinputstream("text1.txt"); bufferedreader br = new bufferedreader(new inputstreamreader(in)); string strline; while((strline = br.readline())!= null) { // system.out.println(strline); list.add(strline); //system.out.print(list); } system.out.print(list); collections.sort(list); }catch(exception e){ system.out.println(e); } } } class songdetail { private string song; private string moviename; private string artist; public songdetail { } public void setsong(string song) throws invalidsongexception { this.song=song; { throw new invalidsongexception(); } } public void setmoviename(string moviename) { this.moviename=moviename; } public void setartist(string artist) throws invalidartistexception { this.artist=artist; { throw new invalidmarkexception(); } } public string getsong() { return song; } public string getmoviename() { return moviename; } public string getartist() { return artist; } }
what question? code works fine although should close bufferedreader after while loop with:
br.close();
your list isn't printed alphabetically because print out before sort it. changing following 2 lines should solve problem:
collections.sort(list); system.out.print(list);
Comments
Post a Comment