Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- File file = new File("removeText.txt");
- ArrayList<String> list = new ArrayList<>();
- Scanner input = new Scanner(System.in);
- System.out.println("Enter the word you want to remove");
- String word = input.nextLine();
- try {
- list = readFileRemoveWord(file, word);
- } catch (FileNotFoundException ex) {
- ex.printStackTrace();
- }
- try {
- saveFile(file, list);
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- public static ArrayList<String> readFileRemoveWord(File file, String word) throws FileNotFoundException {
- ArrayList<String> listOfWords = new ArrayList<>();
- Scanner read = new Scanner(file);
- while (read.hasNextLine()) {
- listOfWords.add(read.nextLine().replaceAll(word, "") + "\n");
- }
- return listOfWords;
- }
- public static void saveFile(File file, ArrayList<String> list) throws IOException {
- FileWriter fw = new FileWriter(file);
- try {
- // writing content of the list into the file
- for (int i = 0; i < list.size(); i++) {
- fw.write(list.get(i));
- }
- } finally {
- fw.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement