Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Writing to a text file

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing to a text file

    Hello,

    I'm trying to write to a text file but it's not working and I don't see why. I may be doing this all wrong, I don't know. My code is below, it is still at a skeleton stage.

    The Function InitPerformancesReport correctly creates the file and when I get to WriteToPerformancesReport, it writes "Written to file" to the console so it is not throwing an exception apparently. However, nothing is written to the file. I even tried to add an out.flush(); and out.write(); inside the function just for testing purposes but to no avail. I end up with a 0 byte file. What am I doing wrong ?

    Thank you for your help!!
    public class Monitoring {
    	private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH_mm_ss");
    	private static LocalDateTime now;
    	private static String perfLogFile;
    	private static FileWriter writer;
    	private static BufferedWriter out;
     
    	public static void InitPerformancesReport(String suffix) {
    		now = LocalDateTime.now();
    		perfLogFile = dtf.format(now) + "_" + suffix + ".log";
    		String directoryName = "D:\\TempsDeResponse";
    		File directory = new File(directoryName);
    		File file = new File(directoryName + "\\" + perfLogFile);
     
    		if (!directory.exists()) {
    			directory.mkdir();
    		}
     
    		try {
    			if (file.createNewFile()) {
    				System.out.println("File is created");
    			}
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Cannot create file");
    			e.printStackTrace();
    		}
     
    		try {
    			writer = new FileWriter(perfLogFile);
    			out = new BufferedWriter(writer);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Cannot create writer");
    			e.printStackTrace();
    		}
    	}
     
    	public static void WriteToPerformancesReport(String transactionName, Long transactionTime) {
    		try {
     
    			out.write("TEST");
    			out.newLine();
    			System.out.println("Written to file");
     
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Cannot write to file");
    			e.printStackTrace();
    		}
     
    	}
     
    	public static void ExportPerformancesReport() {
    		try {
    			out.close();
    			writer.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}		
    }
    Last edited by Norm; March 12th, 2018 at 12:20 PM. Reason: Added code tag

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a text file

    I've added code tags.
    Please next time wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The code needs import statements and a main() method so that it can be compiled and executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing to a text file

    Quote Originally Posted by Norm View Post
    I've added code tags.
    Please next time wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The code needs import statements and a main() method so that it can be compiled and executed for testing.


    Hello,

    Sorry about the code tags. This was my first post and I didn't know about them.

    Yeah, I guess it does need import statements and a main() method.

    --- Update ---

    With imports and main(). It creates the file but it doesn't write in it. Obviously, I'm doing something wrong but I don't know what :-(

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
     
    public class Monitoring {
     
    	private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH_mm_ss");
    	private static LocalDateTime now;
    	private static String perfLogFile;
    	private static FileWriter writer;
    	private static BufferedWriter out;
     
    	public static void InitPerformancesReport(String suffix) {
    		now = LocalDateTime.now();
    		perfLogFile = dtf.format(now) + "_" + suffix + ".log";
    		String directoryName = "C:\\TempsDeResponse";
    		File directory = new File(directoryName);
    		File file = new File(directoryName + "\\" + perfLogFile);
     
    		if (!directory.exists()) {
    			directory.mkdir();
    		}
     
    		try {
    			if (file.createNewFile()) {
    				System.out.println("File is created");
    			}
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Cannot create file");
    			e.printStackTrace();
    		}
     
    		try {
    			writer = new FileWriter(perfLogFile);
    			out = new BufferedWriter(writer);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Cannot create writer");
    			e.printStackTrace();
    		}
    	}
     
    	public static void WriteToPerformancesReport(String transactionName, Long transactionTime) {
    		try {
     
    			out.write("TEST");
    			out.newLine();
    			System.out.println("Written to file");
     
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Cannot write to file");
    			e.printStackTrace();
    		}
     
    	}
     
    	public static void ExportPerformancesReport() {
    		try {
    			out.close();
    			writer.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}		
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		InitPerformancesReport("EPREMIUM");
    		WriteToPerformancesReport("Test", (long)2000);
    		ExportPerformancesReport();
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a text file

    Where does the out instance write to?
    The program is writing a file, but not where you are looking for it.

    Note: Method names should begin with lowercase letters. Class names begin with uppercase.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing to a text file

    Quote Originally Posted by Norm View Post
    Where does the out instance write to?
    The program is writing a file, but not where you are looking for it.

    Note: Method names should begin with lowercase letters. Class names begin with uppercase.

    Hello,

    Yeah I see. I'm declaring the variable file to hold path + filename but then I'm not using it when I create the writer. Thank you for your help.

    Thanks also for the tip on coding standards, I will review them. Java is definitely not my usual programming language.

    Thank you,
    Muriel

  6. #6
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Writing to a text file

    Thank you. I may have some input. Is this class file part of a package that will perform the function? a copy() method already exist in another file. java.lang.*.

Similar Threads

  1. Writing Image to Text File
    By Nithyanathan Naiker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:25 AM
  2. Writing to output text file
    By gatorsgirl in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2012, 08:17 PM
  3. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  4. Help writing to a text file on a website!
    By straw in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 11:02 AM
  5. Issues with writing to text file
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 12th, 2011, 09:43 AM