Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-03-2009
Member
 
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:

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

Quote:
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?
Reply With Quote
  #2  
Old 26-03-2009
Member
 
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 ()

Quote:
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 
           )
Reply With Quote
  #3  
Old 26-03-2009
Member
 
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 (); 
  )
Reply With Quote
  #4  
Old 26-03-2009
Member
 
Join Date: Oct 2008
Posts: 167
Re: Read txt and maintain the same formatting in JAVA

Quote:
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?
Reply With Quote
  #5  
Old 26-03-2009
Member
 
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 '\'.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Read txt and maintain the same formatting in JAVA"
Thread Thread Starter Forum Replies Last Post
Java will not read input rACHAEL Software Development 5 16-10-2010 09:40 AM
Read and Write a .txt file with Java Mulan Software Development 4 28-07-2010 02:03 AM
What is Scanning and Formatting in Java? Rob Dizzle Software Development 4 18-02-2010 06:49 AM
Formatting time using java.util Remedy Software Development 5 09-02-2010 03:21 AM
Scanner can't read in java cyber-noob Software Development 3 14-11-2009 12:42 PM


All times are GMT +5.5. The time now is 10:52 AM.