Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: file reading & writing

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default file reading & writing

    Sorry to bother you guys, I have simly got a few basic questions.

    1. Program gets the strings entered in the 3 textboxes and writes them to a file, How do i do some sort of check if the line has data writen to it so it doesnt overright the existing first line (save to the next blank line)

    2. Once the user clicks on the "Find Entry" button i would like to search the file for the client name which is the first textbox.

    here is my code:

    package Project;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    /**
     * This program will recieve the IP, PCI and client name of client computers and save them to a text file.
     * 
     * @author Marko Klesnik
     * @version 1.0
     */
     
    public class project implements ActionListener
    {
     
        // Variable Declarations
        // Declare JButtons
        public JButton btnNew, btnSave, btnFind, btnDelete, btnExit;
     
        // Declare JLabels
        public JLabel lblName, lblIP, lblPCI, lblDesc, lblDesc2, lblDesc3;
     
        // Declare JTextFields
        public JTextField txtName, txtIP, txtPCI;
     
    	// Declare a scanner
        private Scanner scanRead;
     
        // Declare a formatter
        private Formatter formatWrite;
     
        // Create a string array
        public String [] clientList;
     
     
        public static void main(String[] args)
            {
                project test = new project();
                test.init();
     
            }
     
     
        public void init ()
        {
            // Define default JButton data
            btnNew = new JButton("New Entry ");
            btnNew.setBackground(Color.BLACK);
            btnNew.setForeground(Color.GREEN);
            btnSave = new JButton("Save Entry");
            btnSave.setBackground(Color.BLACK);
            btnSave.setForeground(Color.GREEN);
            btnFind = new JButton("Find Entry");
            btnFind.setBackground(Color.BLACK);
            btnFind.setForeground(Color.GREEN);
            btnDelete = new JButton("Delete Entry");
            btnDelete.setBackground(Color.BLACK);
            btnDelete.setForeground(Color.GREEN);
            btnExit = new JButton("Exit System");
            btnExit.setBackground(Color.BLACK);
            btnExit.setForeground(Color.RED);
     
            // Define default JLabel data
            lblName = new JLabel("Client Name:");
            lblName.setForeground(Color.WHITE);
            lblIP = new JLabel("IP Address:");
            lblIP.setForeground(Color.WHITE);
            lblPCI = new JLabel("PC Identifier:");
            lblPCI.setForeground(Color.WHITE);
            lblDesc = new JLabel(" This program will allow the user to add ");
            lblDesc.setForeground(Color.WHITE);
            lblDesc2 = new JLabel(" edit or delete Users.their PC Identifiers ");
            lblDesc2.setForeground(Color.WHITE);
            lblDesc3 = new JLabel(" and their PC's IP Address. ");
            lblDesc3.setForeground(Color.WHITE);
     
            //Define default JTextField data
            txtName = new JTextField("Client1", 10);
            txtName.setBackground(Color.BLACK);
            txtName.setForeground(Color.GREEN);
            txtIP = new JTextField("", 10);
            txtIP.setBackground(Color.BLACK);
            txtIP.setForeground(Color.GREEN);
            txtPCI = new JTextField("", 10);
            txtPCI.setBackground(Color.BLACK);
            txtPCI.setForeground(Color.GREEN);
     
            //Create container + Layout
            SpringLayout scnLayout = new SpringLayout();
     
            // Create a frame and apply settings
            JFrame progFrame = new JFrame("[COMPANY NAME] Client Recording Software");
            progFrame.setVisible(true);
            progFrame.setSize(900, 800);
            progFrame.setResizable(false);
     
            // Create a JPanel and apply settings
            JPanel panel1 = new JPanel();
            panel1.setVisible(true);
            panel1.setSize(200, 300);
            panel1.setBackground(Color.black);
            panel1.setLayout(scnLayout);
     
            // Add panels to frame
            progFrame.add(panel1);
     
            // Add components to panels
            panel1.add(lblName);
            panel1.add(txtName);
            panel1.add(btnNew);
     
            panel1.add(lblPCI);
            panel1.add(txtPCI);
            panel1.add(lblIP);
            panel1.add(txtIP);
            panel1.add(btnSave);
            panel1.add(btnFind);
            panel1.add(btnDelete);
            panel1.add(btnExit);
            panel1.add(lblDesc);
            panel1.add(lblDesc2);
            panel1.add(lblDesc3);
     
            // Set locations of panel1 components
            scnLayout.putConstraint(SpringLayout.WEST, lblName, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblName, 200, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, txtName, 200, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, txtName, 35, SpringLayout.EAST, lblName);
            scnLayout.putConstraint(SpringLayout.NORTH, btnNew, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, btnNew, 25, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblPCI, 250, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblPCI, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, txtPCI, 250, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, txtPCI, 35, SpringLayout.EAST, lblPCI);
            scnLayout.putConstraint(SpringLayout.NORTH, lblIP, 300, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblIP, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, txtIP, 300, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, txtIP, 45, SpringLayout.EAST, lblIP);
            scnLayout.putConstraint(SpringLayout.NORTH, btnSave, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, btnSave, 150, SpringLayout.EAST, btnNew);
            scnLayout.putConstraint(SpringLayout.NORTH, btnFind, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.EAST, btnFind, -150, SpringLayout.WEST, btnDelete);
            scnLayout.putConstraint(SpringLayout.NORTH, btnDelete, 10, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.EAST, btnDelete, -25, SpringLayout.EAST, panel1);
            scnLayout.putConstraint(SpringLayout.EAST, btnExit, -25, SpringLayout.EAST, panel1);
            scnLayout.putConstraint(SpringLayout.SOUTH, btnExit, -20, SpringLayout.SOUTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc, 80, SpringLayout.NORTH, panel1);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc2, 350, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc2, 10, SpringLayout.SOUTH, lblDesc);
            scnLayout.putConstraint(SpringLayout.WEST, lblDesc3, 385, SpringLayout.WEST, panel1);
            scnLayout.putConstraint(SpringLayout.NORTH, lblDesc3, 10, SpringLayout.SOUTH, lblDesc2);
     
            btnExit.addActionListener(this);
            btnNew.addActionListener(this);
            btnSave.addActionListener(this);
            btnFind.addActionListener(this);
            btnDelete.addActionListener(this);
     
        }
     
     
     
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // Set what haphens on button clicks
            //New Entry button clicked
           if (btnNew.hasFocus())
           {
           	txtName.setText("");
           	txtPCI.setText("");
           	txtIP.setText("");
           }
     
          //Save Entry button clicked
          if (btnSave.hasFocus())
          {
          	try
          	{
          		formatWrite = new Formatter("ipaddress.txt");
          	}
          	catch (Exception outputTC)
          	{
          		System.out.println("Cannot write to file");
          	}
     
          		formatWrite.format("%s%s%s", txtName.getText() + " ", txtPCI.getText() + " ", txtIP.getText());
          		formatWrite.close();	
          }
     
           //Find Entry button clicked
          if (btnFind.hasFocus())
          {
          	try
          	{
          	scanRead = new Scanner(new File("ipaddress.txt"));
          	}
          	catch(Exception inTC)
          	{
          		System.out.println("Could not find file");
          	}
     
          	while(scanRead.hasNext())
          	{
          		String readA = scanRead.next();
          		String readB = scanRead.next();
          		String readC = scanRead.next();
     
          		txtName.setText(readA);
          		txtPCI.setText(readB);
          		txtIP.setText(readC);
     
          	}
          	scanRead.close();
          }
     
          //Delete Entry button clicked
          if (btnDelete.hasFocus())
          {
     
          }
     
           //Exit button clicked
          if (btnExit.hasFocus())  { System.exit(0); }
        }
    }

    Thanks in advance


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: file reading & writing

    Quote Originally Posted by macko View Post
    1. Program gets the strings entered in the 3 textboxes and writes them to a file, How do i do some sort of check if the line has data writen to it so it doesnt overright the existing first line (save to the next blank line)
    The FileOutputStream has a constructor that takes a boolean- whether the file should be overwritten or appended. That's a good google keyword, something like "java file io append" would probably get you some promising results.

    Quote Originally Posted by macko View Post
    2. Once the user clicks on the "Find Entry" button i would like to search the file for the client name which is the first textbox.
    What's your question with this? What have you tried? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: file reading & writing

    Just trying to actualy search the file for a string i guess i am then going to display it in the txtfields.

    I know how to do it prety much just curious on some ideas on how to actualy search the file for the string such as Client1, Client2 etc i then want to set the 3 values on that line (Name, PCI, IP) to string variables and ill then display them in the 3 text boxes. So to clearly explain id like to search for a string in the file and set each of the 3 values on that line to a variable. And im guessing i would use split to sperate them, Just need knoledge on search for a string.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: file reading & writing

    Well, you could either read the file line by line, searching each line, or you could read the whole file into a data structure of some kind, then search that. There are a ton of ways to do it, it's really up to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: file reading & writing

    ah ye dont worry was a stupid question i had'nt even sat down and thought about it lol have had a few beers so sorry.

    Could use an if statement such as if (searchString.Exists) that would work if im not mistaken

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: file reading & writing

    Quote Originally Posted by macko View Post
    ah ye dont worry was a stupid question i had'nt even sat down and thought about it lol have had a few beers so sorry.

    Could use an if statement such as if (searchString.Exists) that would work if im not mistaken
    Hmm, maybe, it really depends on what you're doing. I don't know what Exists is, but I'm assuming that's pseudocode for a real method. What happened when you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: file reading & writing

    Im trying to get the append to work first, It doesnt seem to work as you see i have used

    formatWrite = new Formatter("ipaddress.txt");

    it gives a syntax error for

    formatWrite = new Formatter("ipaddress.txt", true);

    Is there anyway to get the append to work with a formatter?
    have googled the coders are showing a few different examples such as creating a new class etc.. But before i would have to do that or make the code longer then it has to be i figured id ask if there is a way to get it working with the formatter.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: file reading & writing

    I don't really know what you're trying to do with the Formatter. I'd recommend just using a FileOutputStream at first. What does the Formatter give you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Reading a writing binary file
    By stu1811 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 30th, 2010, 12:58 PM
  2. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  3. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  4. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM
  5. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM