Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By SurfMan

Thread: I want to make a record of these logs...

  1. #1
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Red face ??Save this info to a text file??

    I have a app for exercising.
    I take inputs and assign them to class Variables.

    I want to save the information every-time I run the app, maybe to a text file, so I can review it later.

    ??Do you have any ideas???
    Java Code:
    //Main loading class
    
    
    public class WeightPlan {
    
    	public static void main(String[] args) {
    		WeightPlanStats day1 = new WeightPlanStats();
    		
    		day1.setStats();
    		day1.showStats();
    		
    	}
    
    }
    Java Code:
    //Do stuff class
    import java.util.Scanner;
    
    class WeightPlanStats {
    		Scanner inputs = new Scanner(System.in);
    	
    		String BP = "Bench-Presses";
    		String CU = "Curls";
    		String SU = "Sit-Ups";
    	
    		int BPs;
    		int CUs;
    		int SUs;
    		
    		
    		void setStats() {
    	
    			System.out.println("How many Bench-Presses did you complete today?: ");
    			BPs = inputs.nextInt();
    			
    			System.out.println("How many Curls did you complete today?: ");
    			CUs = inputs.nextInt();
    		
    			System.out.println("How many Sit-Ups did you complete today?: ");
    			SUs = inputs.nextInt();
    		
    		}
    	
    		void showStats() {
    			System.out.println(BP + BPs);
    			System.out.println(CU + CUs);
    			System.out.println(SU + SUs);
    		
    		}
    
    
    }
    Last edited by AmericanPi; 12-19-2016 at 08:56 PM. Reason: 65 views and no response, made it shorter.

  2. #2
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: I want to make a record of these logs...

    So...
    I think it has something to do with....
    https://docs.oracle.com/javase/tutor.../io/index.html
    Basic I/O. But i'm not sure.

    I'm not asking for code or an answer. I'm asking for the name of how it's done-I can, and will, go learn it but need to know what it is.
    Tons of docs on Oracle.org, whittling down would help.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: I want to make a record of these logs...

    You can use a BufferedWriter to write stuff out to a file. It might look a bit weird, but this is basically how it works:
    Java Code:
            try {
                BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(System.getProperty("user.dir"), "myfile.txt"));
    
                bufferedWriter.write(myString1);
                bufferedWriter.write(myString2);
                bufferedWriter.write(myString3);
    
                bufferedWriter.flush();
                bufferedWriter.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    The reverse is to read the file using a BuffererdReader:
    Java Code:
    try {
                BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(System.getProperty("user.dir"), "myfile.txt"));
    
                String myString1 = bufferedReader.readLine();
                String myString2 = bufferedReader.readLine();
                String myString3 = bufferedReader.readLine();
    
                bufferedReader.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    Also, if the file is not too big, use the method readAllLines:
    Java Code:
    List<String> lines = Files.readAllLines(Paths.get(System.getProperty("user.dir"), "myfile.txt"));
    If it's settings you want to store, using a Properties file is even easier:
    Java Code:
    Properties props = new Properties();
    //store the settings
    props.store(Files.newBufferedWriter(Paths.get(System.getProperty("user.dir"), "myfile.txt")), "");
    //load the settings
    props.load(Files.newBufferedReader(Paths.get(System.getProperty("user.dir"), "myfile.txt")));
    As you can see, there are various ways to do this.
    AmericanPi likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    AmericanPi is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: I want to make a record of these logs...

    Oh Thank Java!
    That sounds cool.

    @SurfMan Thanks for the code, that helps allot!

Similar Threads

  1. Replies: 5
    Last Post: 05-14-2016, 09:29 PM
  2. Logs
    By estrellab612 in forum New To Java
    Replies: 1
    Last Post: 11-07-2014, 09:47 PM
  3. Emailing error logs
    By renolovexoxo in forum New To Java
    Replies: 1
    Last Post: 02-03-2014, 05:41 AM
  4. Replies: 9
    Last Post: 07-09-2011, 07:15 PM
  5. how to get lucene logs
    By virendra in forum Lucene
    Replies: 0
    Last Post: 01-06-2010, 10:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •