Q:

Writng to a txt

I have a txt file, I want to scan it and get from it a specific data and write the result in another txt file.
It works for my with system print line only.
Writing in the new txt file I created doesn't work for me, I would appreciate if someone can let me know what I'm missing !
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class ReadingFile {

@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) throws Exception {
    // reading a current txt file
    File file = new File("C:/Users/Aboelmagd/Desktop/myfile1.txt");
    FileReader reader = new FileReader(file);
    BufferedReader br = new BufferedReader(reader);
    String reading;

    // Creating new txt to get the result
    File newFile = new File("C:/Users/Aboelmagd/Desktop/my.txt");
    FileWriter writer = new FileWriter(newFile);
    BufferedWriter bw = new BufferedWriter(writer);
    String writing;
    Scanner sc = new Scanner(reader);

    // Scanning myFile.txt and writing to my.txt
    while (sc.hasNext()) {
        writing = sc.next();
        // copying the full of myFile.txt to my.txt
        //bw.write(writing); 
        //bw.newLine();
        // If type is found, cet it and write to my.txt
        if (writing.contains("Type:")) {
            writing = writing.substring(0);
            // printing is working, yohooo :D
            System.out.println(writing);
            // writing to the my.txt is not working ?!!!
            bw.write(writing);
        }
    } 
  }
}

A:

Unless and until you call the close() method of the writer object, the contents will not be physically written to the real file. Always, close the writer after your writing tasks are over. In your case, call
 bw.close();

after your while loop closes.

