Results 1 to 5 of 5

Thread: Read txt and maintain the same formatting in JAVA

  1. #1
    Join Date
    Feb 2009
    Posts
    81

    Read txt and maintain the same formatting in JAVA

    Hello, I don't understand how can I keep the same format a. Txt

    Virtually the read, I put in a string and then put it here:

    JTextArea textarea = new JTextArea (string);
    I read the file with this code:

    While (filebuffer1.ready ()) (
    lic + = (filebuffer1.readLine ());
    )
    them is very likely that mistake.

    There are other components, or methods of reading, which retain the same formatting of source files?

  2. #2
    Join Date
    Oct 2008
    Posts
    167

    Re: Read txt and maintain the same formatting in JAVA

    Assuming that "filebuffer1" is a BufferedReader, why not get what they want is the way in which the method. ReadLine ()

    public String ReadLine ()
    throws IOException

    Reads a line of text. A line is considered to be terminated by any one of a line feed ( '\ n'), a carriage return ( '\ r'), or a carriage return followed immediately by a linefeed.

    Returns:
    A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
    Throws:
    IOException - If an I / O error occurs
    Law and a "single line" and you will return without the carriage return characters from a String object.
    You must enter you the terminator line ending at the bottom of the String received.

    To avoid mile-milla String (the + operator applied to two creates a third String String that is the result of the first two concatenated in Java String objects are immutable) in your cycle you can declare object "lic" as a StringBuilder and use his method. append () to concatenate the String returned by the BufferedReader, for example:

    Code:
     Lic StringBuilder = new StringBuilder; 
    
      while (filebuffer1.ready ()) ( 
                 String line = filebuffer1.readLine ();  
        	      lic.append (line) / / concatenate the string read from the buffer 
                 lic.append ( "\ n"); / / concatenate the character to end of line 
               )

  3. #3
    Join Date
    Dec 2008
    Posts
    183

    Re: Read txt and maintain the same formatting in JAVA

    Read the text file all at once.

    Code:
     Scanner in = new Scanner (txt file). UseDelimiter ( "\ \ Z"); 
      try ( 
          String fulltext = in.next (); 
      ) Finally ( 
          in.close (); 
      )

  4. #4
    Join Date
    Oct 2008
    Posts
    167

    Re: Read txt and maintain the same formatting in JAVA

    Read the text file all at once.

    Code:
    Scanner in = new Scanner (txt file). UseDelimiter ( "\ \ Z"); 
      try ( 
          String fulltext = in.next (); 
      ) Finally ( 
          in.close (); 
      )

    Good to know ...
    What is the string passed as a parameter to the method useDelimiter ()? A regular expression?

  5. #5
    Join Date
    Dec 2008
    Posts
    183

    Re: Read txt and maintain the same formatting in JAVA

    Yes, it is a regular expression which belongs to the species (rare) of regular expressions that you understand ".

    \ Z means "until the end of the input, except the character that signals the end of the input, if any."

    Becomes "\ \ Z" because \ is a special character in Java strings and can type in a string "must use the escape '\'.

Similar Threads

  1. Java will not read input
    By rACHAEL in forum Software Development
    Replies: 5
    Last Post: 16-10-2010, 09:40 AM
  2. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  3. What is Scanning and Formatting in Java?
    By Rob Dizzle in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 06:49 AM
  4. Formatting time using java.util
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 03:21 AM
  5. Scanner can't read in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 12:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,183,476.65683 seconds with 17 queries