java read files from directory iteratively and assign values separate arraylists -
i read txt files iteratively , want read contents of each file , assign content arraylists separately. code use reaching txt files :
path basepath = paths.get("filepath"); try (directorystream<path> pathlist = files.newdirectorystream(basepath, "*.txt")) { (path path : pathlist) { system.out.println(path.tostring()); } } catch (ioexception e) { e.printstacktrace(); }
i'm not sure understood question.
use bufferedreader read contents of files line line, there can add lines arraylists.
path basepath = paths.get("filepath"); try (directorystream<path> pathlist = files.newdirectorystream(basepath, "*.txt")) { (path path : pathlist) { bufferreader reader = files.newbufferedreader(path, standardcharsets.utf_8); string currentline = reader.readline(); arraylist<string> comments = new arraylist<>(); while( currentline != null ) { comments.add(currentline); currentline = reader.readline(); } } } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment