>

Saturday, December 15, 2012

Java I\O Part II

In the previous post we have try how byte streams process, now we continue for character oriented, how it work same but have different class.

A. Character Streams
Text in java presented as set char which values two-byte characters, based on unicode standard, some of the standard charsets are US-ASCII, UTF-8, and UTF-16. The Java classes FileReader and FileWriter were specifically created to work with text files, but they work only with default character encoding and don’t handle localization properly. According the best way is to pipe class InputStreamReader with specified encoding and the FileInputStream then InputStreamReader reads bytes and decodes them into characters using a specified CharSet, for FileOutputStream and OutputStreamWriter are same treatment as FileInputStream. The class InputStreamReader .

Let’s try for writing a text file, we determine what we will put in our text file :

String myWrting ="Hi guys! This is my file text"

we use FileOutputStream and OutputStreamWriter, I pipe The class FileOutputStream reads bytes and decodes them into characters using a specified CharSet. “UTF8”

FileOutputStream myFile = new FileOutputStream("myWrting.txt");
Writer out = new BufferedWriter(new OutputStreamWriter(myFile,"UTF8"));

and here its our full code,

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class WrittingTxtDemo {

public static void main(String[] args) {
 try {
 String myWrting ="Hi guys! This is my file text";
 FileOutputStream myFile = new FileOutputStream("myWrting.txt");

 Writer writer = new BufferedWriter(new  
 OutputStreamWriter(myFile, "UTF8"));

  writer.write(myWrting);
  writer.flush();
  writer.close();

  } catch (Exception e) {
      e.printStackTrace();
    }
  }
}



you may open the file we have created by notepad, and the contain will the same as we put in the code above.

Now we create how to read file text file in java, reads bytes from a text file and converts them from UTF-8 encoding into Unicode to return results as a String, I recommended uses StringBuffer that usually works faster than String when it comes to performing text manipulations. I used file that has been created before.


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class ReadingTxtDemo {
public static void main(String[] args) {
  StringBuffer buffer = new StringBuffer();

  try {
    FileInputStream myFile = new FileInputStream("myWrting.txt");
    InputStreamReader input = new InputStreamReader(myFile,  
    "UTF8");
    Reader reader = new BufferedReader(input);
 
     int ch;
   
     while ((ch = reader.read())> -1) {
      buffer.append((char) ch);
     }

      buffer.toString();
      System.out.println("The file contains : "+buffer);
     } catch (Exception e) {
      System.out.println(e);
     }
   }
}

No comments:

Post a Comment