Results 1 to 6 of 6
  1. #1
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Reading a text file and creating a String from contents

    Hello, I am trying to make a program that will read the first line of a .txt file and the assign that line an identifier.. if that makes sense?

    so it will read the first line of *.txt (say the first line is qwerty) it will then make 'String input = qwerty <--- first line of .txt

    I am using this code at the moment to read the file and print the text, how should i go about modifying it?
    Java Code:
    //Directory
    String path = "C:\\Home_Directories\\" + gradYear + "\\" + username + "\\" + username + ".dat";
      
        File f = new File(path);
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
    
        StringBuffer sb = new StringBuffer();
        String eachLine = br.readLine();
    
        while (eachLine != null) {
          sb.append(eachLine);
          sb.append("\n");
          eachLine = br.readLine();
        }
    	
        System.out.println(sb.toString());
    }
    }
    Thanks for any help :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Reading a text file and creating a String from contents

    I dont understand your problem ?! Why do you read the whole file if you only want the first line ?!

    String eachLine = br.readLine(); is exactly what you want or? --> so it will read the first line of *.txt (say the first line is qwerty) it will then make 'String input = qwerty <--- first line of .txt (rename eachLine to input :D)

    ??

  3. #3
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Reading a text file and creating a String from contents

    Hi yes sorry I did a basic tutorial for reading a .txt file (which is the above code).
    I'm now trying to edit that code 1- to read only the first line. 2- instead of printing the line to assign a name to the data such as 'input.'

    So i can then add an 'if' to perform a certain action depending on whether the first line matches a user input.

    Thanks.
    Last edited by paulio2; 11-17-2011 at 04:34 PM.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Reading a text file and creating a String from contents

    sorry, I still do not understand what your problem is :(
    Delete the lines 11-17 and you will get what you want ?!

  5. #5
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Reading a text file and creating a String from contents

    Ok i'll give you my full code, maybe you'll see. the user will enter a password.. this is checked against the first line of the .dat/.txt file.

    [CODE]

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import javax.swing.JOptionPane;

    public class reader {
    public static void main(String[] argv) throws Exception {

    boolean status;
    // Prompt for Student Username
    String username = JOptionPane.showInputDialog(null, "What is the Students Username?");

    // Prompt for Student Year Level
    String gradYear = JOptionPane.showInputDialog(null, "What is the Students grad year?");

    //Prompt for password
    String password = JOptionPane.showInputDialog(null, "Password?");

    //Directory
    String path = "C:\\Home_Directories\\" + gradYear + "\\" + username + "\\" + username + ".dat";

    File f = new File(path);
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);

    StringBuffer sb = new StringBuffer();
    String eachLine = br.readLine();

    while (eachLine != null) {
    sb.append(eachLine);
    sb.append("\n");
    eachLine = br.readLine();
    }

    System.out.println(sb.toString());
    }
    }
    [CODE]

    thank you.

  6. #6
    paulio2 is offline Member
    Join Date
    Nov 2011
    Location
    U.K
    Posts
    27
    Rep Power
    0

    Default Re: Reading a text file and creating a String from contents

    Oh thank you i see it now :D

    thanks for your help lol.

Similar Threads

  1. Replies: 6
    Last Post: 03-12-2010, 04:54 AM
  2. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  3. Replies: 6
    Last Post: 12-12-2008, 03:35 PM
  4. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:00 AM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 02:29 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
  •