banner



How To Replace An Architectural Register File With A Physical Register File In Java

In this tutorial, we show y'all how to read from and write to text (or character) files using classes available in the java.io packet. First, let's look at the different classes that are capable of reading and writing grapheme streams.

ane. Reader, InputStreamReader, FileReader and BufferedReader

Reader is the abstract class for reading character streams. It implements the post-obit key methods:

  • read() : reads a single character.
  • read(char[]) : reads an assortment of characters.
  • skip(long) : skips some characters.
  • close() : closes the stream.

InputStreamReader is a span from byte streams to character streams. It converts bytes into characters using a specified charset. The charset tin can be default character encoding of the operating system, or can be specified explicitly when creating an InputStreamReader .

FileReader is a convenient class for reading text files using the default character encoding of the operating system.

BufferedReader reads text from a grapheme stream with efficiency (characters are buffered to avoid oft reading from the underlying stream) and provides a convenient method for reading a line of text readLine() .

The following diagram show human relationship of these reader classes in the java.io package:

Reader Hierarchy

2. Writer, OutputStreamWriter, FileWriter and BufferedWriter

Author is the abstruse class for writing graphic symbol streams. It implements the following fundamental methods:

  • write(int) : writes a single graphic symbol.
  • write(char[]) : writes an array of characters.
  • write(String) : writes a string.
  • close() : closes the stream.

OutputStreamWriter is a bridge from byte streams to graphic symbol streams. Characters are encoded into bytes using a specified charset. The charset tin can be default character encoding of the operating system, or can exist specified explicitly when creating an OutputStreamWriter .

FileWriter is a convenient class for writing text files using the default graphic symbol encoding of the operating system.

BufferedWriter writes text to a character stream with efficiency (characters, arrays and strings are buffered to avoid frequently writing to the underlying stream) and provides a convenient method for writing a line separator: newLine() .

The following diagram show relationship of these writer classes in the coffee.io package:

Writer Hierarchy

3. Character Encoding and Charset

When amalgam a reader or writer object, the default character encoding of the operating system is used (e.thousand. Cp1252 on Windows):

FileReader reader = new FileReader("MyFile.txt"); FileWriter writer = new FileWriter("YourFile.txt");

So if we want to use a specific charset, utilize an InputStreamReader or OutputStreamWriter instead. For instance:

InputStreamReader reader = new InputStreamReader( 					new FileInputStream("MyFile.txt"), "UTF-16");

That creates a new reader with the Unicode character encoding UTF-xvi.

And the post-obit statement constructs a writer with the UTF-8 encoding:

OutputStreamWriter author = new OutputStreamWriter( 					new FileOutputStream("YourFile.txt"), "UTF-8");

In example we want to use a BufferedReader , only wrap the InputStreamReader inside, for case:

InputStreamReader reader = new InputStreamReader( 		new FileInputStream("MyFile.txt"), "UTF-16");  BufferedReader bufReader = new BufferedReader(reader);

And for a BufferedWriter example:

OutputStreamWriter writer = new OutputStreamWriter( 					new FileOutputStream("YourFile.txt"), "UTF-8");  BufferedWriter bufWriter = new BufferedWriter(author);

At present, let's look at some complete examples.

4. Coffee Reading from Text File Case

The following small-scale program reads every single character from the file MyFile.txt and prints all the characters to the output console:

package net.codejava.io;  import java.io.FileReader; import java.io.IOException;  /**  * This program demonstrates how to read characters from a text file.  * @author www.codejava.net  *  */ public form TextFileReadingExample1 {  	public static void master(String[] args) { 		try { 			FileReader reader = new FileReader("MyFile.txt"); 			int grapheme;  			while ((character = reader.read()) != -1) { 				Arrangement.out.print((char) grapheme); 			} 			reader.close();  		} grab (IOException due east) { 			e.printStackTrace(); 		} 	}  }

The following case reads a text file with assumption that the encoding is UTF-16:

packet net.codejava.io;  import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader;  /**  * This program demonstrates how to read characters from a text file using  * a specified charset.  * @writer world wide web.codejava.net  *  */ public class TextFileReadingExample2 {  	public static void main(String[] args) { 		try { 			FileInputStream inputStream = new FileInputStream("MyFile.txt"); 			InputStreamReader reader = new InputStreamReader(inputStream, "UTF-xvi"); 			int character;  			while ((character = reader.read()) != -1) { 				System.out.impress((char) character); 			} 			reader.close();  		} catch (IOException east) { 			eastward.printStackTrace(); 		} 	}  }

And the following example uses a BufferedReader to read a text file line by line (this is the near efficient and preferred mode):

package net.codejava.io;  import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;  /**  * This program demonstrates how to read characters from a text file  * using a BufferedReader for efficiency.  * @author www.codejava.internet  *  */ public form TextFileReadingExample3 {  	public static void chief(String[] args) { 		try { 			FileReader reader = new FileReader("MyFile.txt"); 			BufferedReader bufferedReader = new BufferedReader(reader);  			Cord line;  			while ((line = bufferedReader.readLine()) != goose egg) { 				Organization.out.println(line); 			} 			reader.shut();  		} catch (IOException e) { 			eastward.printStackTrace(); 		} 	}  }

5. Java Writing to Text File Example

In the following instance, a FileWriter is used to write 2 words "Hello Earth" and "Good Bye!" to a file named MyFile.txt:

bundle net.codejava.io;  import java.io.FileWriter; import java.io.IOException;  /**  * This program demonstrates how to write characters to a text file.  * @author www.codejava.net  *  */ public class TextFileWritingExample1 {  	public static void main(String[] args) { 		try { 			FileWriter writer = new FileWriter("MyFile.txt", true); 			author.write("Howdy Earth"); 			writer.write("\r\n");	// write new line 			author.write("Good Goodbye!"); 			writer.close(); 		} take hold of (IOException e) { 			e.printStackTrace(); 		}  	}  }

Annotation that, a writer uses default grapheme encoding of the operating arrangement past default. Information technology too creates a new file if non exits, or overwrites the existing i. If y'all want to append text to an existing file, laissez passer a boolean flag of true to constructor of the writer grade:

FileWriter writer = new FileWriter("MyFile.txt", true);

The following example uses a BufferedReader that wraps a FileReader to suspend text to an existing file:

bundle net.codejava.io;  import coffee.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException;  /**  * This program demonstrates how to write characters to a text file  * using a BufferedReader for efficiency.  * @author world wide web.codejava.cyberspace  *  */ public class TextFileWritingExample2 {  	public static void master(Cord[] args) { 		attempt { 			FileWriter writer = new FileWriter("MyFile.txt", truthful); 			BufferedWriter bufferedWriter = new BufferedWriter(writer);  			bufferedWriter.write("How-do-you-do Globe"); 			bufferedWriter.newLine(); 			bufferedWriter.write("Come across Y'all Over again!");  			bufferedWriter.close(); 		} catch (IOException eastward) { 			e.printStackTrace(); 		}  	}  }

This is the preferred way to write to text file because the BufferedReader provides efficient way for writing character streams.

And the following case specifies specific character encoding (UTF-16) when writing to the file:

packet net.codejava.io;  import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter;  /**  * This program demonstrates how to write characters to a text file using  * a specified charset.  * @writer www.codejava.internet  *  */ public class TextFileWritingExample3 {  	public static void main(String[] args) { 		endeavour { 			FileOutputStream outputStream = new FileOutputStream("MyFile.txt"); 			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-16"); 			BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); 			 			bufferedWriter.write("Xin chào"); 			bufferedWriter.newLine(); 			bufferedWriter.write("Hẹn gặp lại!"); 			 			bufferedWriter.close(); 		} catch (IOException e) { 			e.printStackTrace(); 		} 		 	} }

This program writes some Unicode string (Vietnamese) to the specified text file.

NOTE: From Java 7, y'all can use try-with-resources statement to simplify the code of opening and closing the reader/writer. For case:

endeavour (FileReader reader = new FileReader("MyFile.txt")) { 	int character;  	while ((character = reader.read()) != -1) { 		Organization.out.print((char) character); 	} } catch (IOException e) { 	eastward.printStackTrace(); }

References:

  • Lesson: Basic I/O (The Java Tutorials)

Related File IO Tutorials:

  • How to Read and Write Binary Files in Java
  • How to read text file line by line in Coffee
  • Coffee IO FileReader and FileWriter Examples

Other Java File IO Tutorials:

  • How to list files and directories in a directory in Java
  • Java IO - Mutual File and Directory Operations Examples
  • Java Serialization Basic Example
  • Understanding Coffee Externalization with Examples
  • How to execute Operating Organization Commands in Java
  • 3 ways for reading user'southward input from console in Java
  • File change notification example with Sentry Service API
  • Java Scanner Tutorial and Lawmaking Examples
  • How to shrink files in ZIP format in Java
  • How to excerpt Zero file in Coffee

About the Author:

Nam Ha Minh is certified Coffee developer (SCJP and SCWCD). He started programming with Java in the fourth dimension of Java one.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you lot YouTube.

Add comment

How To Replace An Architectural Register File With A Physical Register File In Java,

Source: https://www.codejava.net/java-se/file-io/how-to-read-and-write-text-file-in-java

Posted by: grossdayere.blogspot.com

0 Response to "How To Replace An Architectural Register File With A Physical Register File In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel