Results 1 to 3 of 3
  1. #1
    jklittle82 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default File Demonstration

    I cannot get the Contents to show up in the JFrame Window of the file in the FileDemonstration inner class ReadBinaryFile's gamedata.ser.
    Here is my work!
    FileDemonstration.java

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import java.io.EOFException;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.UTFDataFormatException;
    import java.lang.NullPointerException;
    
    public class FileDemonstration extends JFrame 
    {
    	private JTextArea outputArea;
    	private JScrollPane scrollPane;
    	
    	public FileDemonstration()
    	{
    		super("Testing class File");
    		
    		outputArea = new JTextArea();
    		
    		scrollPane = new JScrollPane(outputArea);
    		
    		add(scrollPane, BorderLayout.CENTER);
    		
    		setSize(400, 400);
    		setVisible(true);
    		
    		analyzePath();
    	}
    	
    	private File getFileOrDirectory()
    	{
    		JFileChooser fileChooser = new JFileChooser();
    		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    		
    		int result = fileChooser.showOpenDialog(this);
    		
    		if (result == JFileChooser.CANCEL_OPTION);
    			System.exit(1);
    			
    		File fileName = fileChooser.getSelectedFile();
    		
    		if ((fileName == null) || (fileName.getName().equals("")))
    		{
    			JOptionPane.showMessageDialog(this, "Invalid Name", "Invalid Name", JOptionPane.ERROR_MESSAGE);
    			System.exit(1);
    		}
    		return fileName;
    	}
    	
    	public void analyzePath()
    	{
    		File name = getFileOrDirectory();
    		
    		if (name.exists())
    		{
    			outputArea.setText(String.format("%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s", 
    					name.getName(), " exists",
    					(name.isFile() ? "is a file" : "is not a file"),
    					(name.isDirectory() ? "is absolute path" : 
    						"is not absolute path"), "Last modified: ",
    						name.lastModified(), "Length: ", name.length(),
    						"Path: ", name.getPath(), "Absolute path: ",
    						name.getAbsolutePath(), "Parent: ", name.getParent()));
    			if(name.isDirectory())
    			{
    				String[] directory = name.list();
    				outputArea.append("\n\nDirectory contents:\n");
    				
    				for(String directoryName : directory)
    					outputArea.append(directoryName + "\n");
    			}
    		}
    		else
    		{
    			JOptionPane.showMessageDialog(this, name + 
    					" does not exist.", "ERROR", JOptionPane.ERROR_MESSAGE);
    		}
    	}
    	
    	public class ReadBinaryFile {
    		private ObjectInputStream input;
    		
    		public void openFile()
    		{
    			try
    			{
    				input = new ObjectInputStream(new FileInputStream("gamedata.ser"));
    			}
    			catch(FileNotFoundException fileNotFoundException)
    			{
    				System.out.println("Error opening file: java.io.FileNotFoundException: gamedata.ser <The system cannot " +
    						"the file specified>");
    			}
    			catch(IOException ioException)
    			{
    				System.err.println("Error opening file.");
    				System.exit(1);
    			}
    			
    		}
    		
    		public void readRecords()
    		{
    			GameRecordSerializable record;
    			
    			System.out.printf("%-10s%-12s%-12s%-12s%-12s\n", "Game", "Score_L", "Score_R", "Time_L", "Time_R");
    			
    			
    			try
    			{
    				while(true)
    				{
    					record = (GameRecordSerializable)input.readObject();
    					System.out.printf("%-10d%-12d%-12d%-12.2f%-12.2f\n", record.getGameID(), record.getLeftHandTeam(), record.getRightHandTeam(),
    						record.getControlL(), record.getControlR());
    				}
    			}
    			catch(UTFDataFormatException utfDataFormatException)
    			{
    				System.err.println("Error reading from file: ");
    				
    			}
    			catch(NullPointerException nullPointerException)
    			{
    				System.err.println("Error during reading from file: java.lang.NullPointerException");
    			}
    			
    			catch(EOFException endOfFileException)
    			{
    				return;
    			}
    			catch (IOException ioException)
    			{
    				System.err.println("Error during reading from file: java.io.UTFDataFormatException");
    				
    			}
    			
    			catch(ClassNotFoundException classNotFoundException)
    			{
    				System.err.println("Error during read from file.");
    			}
    			}
    				
    			
    		
    		
    		public void closeFile()
    		{
    			try
    			{
    			if(input != null)
    			{
    				input.close();
    			}
    			}
    			catch(IOException ioException)
    			{
    				System.err.println("Error closing file.");
    				System.exit(1);
    			}
    		}
    	}
    
    }
    FileDemonstrationTest.java

    Java Code:
    import javax.swing.JFrame;
    
    public class FileDemonstrationTest {
    
    	
    	public static void main(String[] args) 
    	{
    		FileDemonstration application = new FileDemonstration();
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    
    }
    GameRecordSerializable.java

    Java Code:
    import java.io.Serializable;
    
    public class GameRecordSerializable implements Serializable 
    {
    	private int gameid;
    	private int leftHandTeam;
    	private int rightHandTeam;
    	private double controlLeftHand;
    	private double controlRightHand;
    	
    	public GameRecordSerializable()
    	{
    		this(0, 0, 0, 0.0, 0.0);
    	}
    
    	public GameRecordSerializable(int gameID, int left, int right, double controlLeft, double controlRight) 
    	{
    		setGameID(gameID);
    		setLeftHandTeam(left);
    		setRightHandTeam(right);
    		setControlL(controlLeft);
    		setControlR(controlRight);
    	}
    	
    	public void setGameID(int gameID)
    	{
    		gameid = gameID;
    	}
    	
    	public int getGameID()
    	{
    		return gameid;
    	}
    	
    	public void setLeftHandTeam(int left)
    	{
    		leftHandTeam = left;
    	}
    	
    	public int getLeftHandTeam()
    	{
    		return leftHandTeam;
    	}
    	
    	public void setRightHandTeam(int right)
    	{
    		rightHandTeam = right;
    	}
    	
    	public int getRightHandTeam()
    	{
    		return rightHandTeam;
    	}
    	
    	public void setControlL(double controlLeft)
    	{
    		controlLeftHand = controlLeft;
    	}
    	
    	public double getControlL()
    	{
    		return controlLeftHand;
    	}
    	
    	public void setControlR(double controlRight)
    	{
    		controlRightHand = controlRight;
    	}
    	
    	public double getControlR()
    	{
    		return controlRightHand;
    	}
    }
    Last edited by JosAH; 11-02-2011 at 06:28 PM. Reason: added [code] ... [/code] tags.

  2. #2
    jMaster's Avatar
    jMaster is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: File Demonstration

    Which IDE are you using?

  3. #3
    jklittle82 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. demonstration
    By jamsheer in forum Advanced Java
    Replies: 0
    Last Post: 03-19-2010, 09:05 AM
  2. ProgressBar Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:52 PM
  3. BusyIndicator Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:32 PM
  4. CheckboxTreeViewer Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:53 PM
  5. TreeEditor Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:53 PM

Posting Permissions

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