java - How to save integers from a .txt file into an array? -
so have array int[] numbers = {1,2}; want 1,2 removed , replaced numbers txt file. can see numbers files in console method:
public string[] readlines(string filename) throws ioexception { filereader filereader = new filereader(filename); bufferedreader bufferedreader = new bufferedreader(filereader); list<string> lines = new arraylist<string>(); string line = null; while ((line = bufferedreader.readline()) != null) { lines.add(line); } bufferedreader.close(); return lines.toarray(new string[lines.size()]); } public static void testfilearrayprovider() throws ioexception { algo1 fap = new algo1(); string[] lines = fap .readlines("d:/users/xxx/workspace/abc/src/abc1/filename123"); (string line : lines) { system.out.println(line); } } now need save them in array. how? xd thx guys
this should work:
// in case populated string[] lines = new string[] {"123", "4567"}; // easier work lists first list<integer> results = new arraylist<>(); (string line : lines) { results.add(integer.parseint(line)); } // if want int[] reason int[] finalresults = new int[results.size()]; (int = 0; < results.size(); i++) { finalresults[i] = results.get(i); } // prove worked system.out.println(arrays.tostring(finalresults)); in java-8, can shorten to
int[] finalresults = arrays.stream(lines).maptoint(integer::parseint).toarray();
Comments
Post a Comment