I've been assigned a Java project by my professor and per usual, he didn't teach us how to use the tools we need to get the job done. So I'm trying to teach myself the basics of file manipulation as well as how to use JFileChooser
, and I'm pretty confused.
In this project, he wanted us to create a JMenuBar
with 3 JMenus
(File, Batch Processing, Interactive Processing). Under the File JMenu
, I have 5 JMenuItems(Open Binary File, Close Binary File, Save Binary File, Create Report, Exit). I listed these to hopefully give an idea of what the app is trying to accomplish.
Here are some snippets of instruction, which is what I've been interpreting and trying to mess around with:
1) Globally declare and create an object of the JFileChooser class: JFileChooser jfc = new JFileChooser();
2) Complete the actionPerformed as follows: for each JMenuItem selected, you will be calling a method or performing an action:
OpenMI: call the OpenFile method to open the binary access file for input
CloseMI: provide warning (JOptionPane) to Save first, if yes, then close the binary file and repaint
SaveMI: call method to open binary file for output then call method to output to it
ReportMI: call method to open text file and output to it
ExitMI: System.exeit(0);
PopulateBatchMI: call method to read from opened binary sequential file and put data into the array of tools objects(a class we created previously)
DisplayMI: display the contents of the toolsarray in a JTextArea on this screen
HideMI: hide the contents of the JTextArea displaying the toolsarray contents
*3) Write each method according to notes given in the text and in the lecture!
Part 4 Next, complete the following: (BSAF = Binary Sequentia Access File)*
1.Write a method called SaveBSAFile and within it a. Create an object of the DataOutputStream b. Create an object of the JFileChooser. Include all JFileChooser code, use the Save option 1. Open the binary sequential file for output 2. print a message to state that it is opened (use a JOptionPane) c. call the SaveBSAFile method when the SaveFile menu item is clicked
I'm having trouble understanding how to use the FileInputStream
/FileOutputStream
DataInputStream/DataOutputStream
classes in conjunction with JFileChooser
. All I've learned so far has been off of this site or others, so my understanding of how these classes work, especially together, has me confused. If anyone wanted to give me an explanation, I would really appreciate it.
Here is the code I've been working with so far, although it doesn't seem to be working correctly. There is an actionPerformed()
that is used when the user clicks one of the menu options, and calls the methods I'm trying to write here:
public void saveBSAFile()
{
filename = JOptionPane.showInputDialog("Please specify a file name for the "
+ " file you wish to save");
try
{
FileOutputStream FOStream = new FileOutputStream(filename);
DataOutputStream DOStream = new DataOutputStream(FOStream);
}
catch (FileNotFoundException e)
{
System.out.println("file name was not found");
}
jfc.setDialogTitle("Specify a file to save");
int userSelection = jfc.showSaveDialog(this);
if (userSelection == jfc.APPROVE_OPTION)
{
File filename = jfc.getSelectedFile();
JOptionPane.showMessageDialog(null, "File to save " + filename,
"Save Review", JOptionPane.INFORMATION_MESSAGE);
}
else if (userSelection == jfc.CANCEL_OPTION)
{
return;
}
}
public void openBSAFile()
{
int status = jfc.showOpenDialog(this);
if (status == JFileChooser.APPROVE_OPTION)
{
File selectedFile = jfc.getSelectedFile();
filename = selectedFile.getAbsolutePath();
JOptionPane.showMessageDialog(null, "You selected " + filename,
"File Review",
JOptionPane.INFORMATION_MESSAGE);
try
{
FileInputStream fstream = new FileInputStream(filename);
DataInputStream dstream = new DataInputStream(fstream);
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "File not found. Please select a " +
"valid file to open", "File Not found",
JOptionPane.ERROR_MESSAGE);
System.err.println(e);
e.printStackTrace();
}
}
}
There are no errors, but nothing seems to be happening. The saveBSAFile method is misleading because it seems its purpose is to create a file and save data from the array of tool objects (which has data members such as ID, price, numberInStock, ect).
No file is created, and when I try to open one I've created and specified, nothing happens, either.
You are opening the streams, but you do not use them. Since the streams are declared in a (
try
-)block, they are only alive and visible in the try-block. Your manipulation of the stream should be happening inside the try-block. Furthermore, you should use the try-with-resources statement. Here you find a tutorial on how to use basic I/O-streams. You can use aJFileChooser
to handle the open dialog.