// Java // make a list reading a txt file import java.io.*; import java.util.*; public class list_from_file_2 { public static void main(String[] args) throws IOException { Scanner scan = new Scanner(new File("list.txt")); List<String> doubles = new ArrayList<String>(); while(scan.hasNext()){ doubles.add(scan.next()); } Collections.sort(doubles); for (String d : doubles) System.out.println(d); } }------------------------------------------------------------------------
// Java // make a list reading a txt file import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class list_from_file { public static void main(String[] args) throws IOException { BufferedReader in = null; FileReader fr = null; List<String> list = new ArrayList<String>(); // String type data read from txt file try { fr = new FileReader("list.txt"); in = new BufferedReader(fr); String str; while ((str = in.readLine()) != null) { list.add(str); // or list.setListData(str); // for jList //list.add(Double.parseDouble(str)); // if read data is double number } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); fr.close(); } for (String d : list) System.out.println(d); } }
Feb 20, 2012
[Java] make a list reading a txt file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment