I have created a personal password storage program that takes the user's username, password, and for what account it is for, and stores it in a text file and can also read from them (assuming you type the right name in). What I'm looking to do, is either trim it down (take out none needed code) or finding more effective ways to go about this.
import java.io.*;
import java.util.*;
public class testing_file
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Access or create?");
String select = input.nextLine();
switch(select)
{
case "Create":
String name, user, password;
FileOutputStream fop = null;
File file;
System.out.println("What account is this for?");
name = input.nextLine();
System.out.println("Enter the Username");
user = input.nextLine();
try
{
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("c:/"+name+".dnx")));
file = new File("c:/"+name+".dnx");
fop = new FileOutputStream(file);
if (!file.exists())
{
file.createNewFile();
}
System.out.println("You may now input your password");
password = input.nextLine();
pw.write("Account: " + name);
pw.println();
pw.write("Username: " + user);
pw.println();
pw.write("Password: " + password);
pw.flush();
pw.close();
System.out.println("Operation Complete");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fop != null)
{
fop.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
break;
case "Access":
String string="";
System.out.println("Enter the account you wish to view");
name = input.nextLine();
String file1 = "c:/"+name+".dnx";
//reading
try{
InputStream ips=new FileInputStream("C:/"+name+".dnx");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String line;
while ((line=br.readLine())!=null)
{
System.out.println(line);
string+=line+"\n";
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
break;
default:
System.out.println("Invalid method, terminating program");
break;
}
}
}
"Access"
case in your program, you needn't write your own program for that. You can use your operating systems standard tools for that;type
command in Windows orcat
in Linux etc. \$\endgroup\$ – abuzittin gillifirca Jun 25 '13 at 12:42