Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1.  
  2. public static void main(String[] args) {
  3. File file = new File("removeText.txt");
  4. ArrayList<String> list = new ArrayList<>();
  5. Scanner input = new Scanner(System.in);
  6. System.out.println("Enter the word you want to remove");
  7. String word = input.nextLine();
  8. try {
  9. list = readFileRemoveWord(file, word);
  10. } catch (FileNotFoundException ex) {
  11. ex.printStackTrace();
  12. }
  13.  
  14. try {
  15. saveFile(file, list);
  16. } catch (IOException ex) {
  17. ex.printStackTrace();
  18. }
  19.  
  20. }
  21.  
  22. public static ArrayList<String> readFileRemoveWord(File file, String word) throws FileNotFoundException {
  23. ArrayList<String> listOfWords = new ArrayList<>();
  24. Scanner read = new Scanner(file);
  25. while (read.hasNextLine()) {
  26. listOfWords.add(read.nextLine().replaceAll(word, "") + "\n");
  27. }
  28. return listOfWords;
  29. }
  30.  
  31. public static void saveFile(File file, ArrayList<String> list) throws IOException {
  32. FileWriter fw = new FileWriter(file);
  33.  
  34. try {
  35. // writing content of the list into the file
  36. for (int i = 0; i < list.size(); i++) {
  37. fw.write(list.get(i));
  38. }
  39. } finally {
  40. fw.close();
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement