Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class FileManager {
- private File file;
- public FileManager(File file){
- this.file = file;
- }
- public ArrayList<String> load() throws Exception {
- ArrayList<String> als = new ArrayList<>();
- Scanner scf = new Scanner(file);
- while(scf.hasNextLine()){
- String line = scf.nextLine().trim();
- if (line.length() > 0)
- als.add(line);
- }
- scf.close();
- return als;
- }
- public void save(ArrayList<String> als) throws Exception {
- FileWriter fw = new FileWriter(file);
- for(int i = 0; i < als.size(); ++i){
- fw.write(als.get(i) + "\n");
- }
- fw.close();
- }
- /*
- public static void main(String [] args){
- try {
- ArrayList<String> als = new ArrayList<>();
- als.add("Labas1");
- als.add("Labas2");
- als.add("Labas3");
- FileManager fm = new FileManager(new File("labas.txt"));
- fm.save(als);
- FileManager fm2 = new FileManager(new File("labas.txt"));
- ArrayList<String> als2 = fm.load();
- FileManager fm3 = new FileManager(new File("labas2.txt"));
- fm3.save(als2);
- } catch(Exception e){
- System.out.println(e);
- }
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement