0

Can someone help me to write a program that can read and write to a file and then store the user input in the file using the Property and ClassLoader classes.Also when another person uses the program it should tell you the last person to use the program.Thanks in advance

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by maingi
0
import java.io.*;
import java.io.File;
import java.util.*;
public class FileReadWrite{

    public static void main(String[] args) throws IOException{

     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
      String name = " "; 

      FileWriter fw = new FileWriter("myFile.properties");//append the file content
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write("Peter maingi"+"\n"); 
      bw.write(name);
      bw.close();
      fw.close();

    //create property object
      Properties props = new Properties();

      InputStream inputStream = ClassLoader.getSystemResourceAsStream("myFile.properties");
      props.load(inputStream);
      String myValue = props.getProperty("key");
      //props.store();

      FileReader fr=new FileReader("myFile.properties");
      BufferedReader br = new BufferedReader(fr);

       System.out.println("Welcome,the name of the last person to use the program was:" + br.readLine());

        while ((name = br.readLine()) != null) { 
            System.out.println("Enter your Name");
            name = stdin.readLine();
           //System.out.println(name);
        }

      br.close();
      fr.close();
  }
}

Edited by Dani: Formatting fixed

0

Rather than having both properties and an extra line with a username, simply add a user=name property. Then all you have to do is load, setProperty (for each key to be changed), and store.

0

I appreciated for ur assistance.The program finnally did wonders.Kudos for ur gud work

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.