wellnwill Marathahalli Bangalore

PrintWriter class


We cannot use FileWriter or BufferedWriter to write primitive data into file and We have to use newLine() method every time to add a new line by writing a line separator.


import java.io.*;
public class WriteToTextFile2
 {
	static FileWriter fw = null;
	static PrintWriter pw = null;
	public static void main(String[] args) 
	 {
		try 
		{
			fw=new FileWriter("MyTextFile.txt");
			pw =new PrintWriter(fw);
			pw.println("Maths Score:");
			pw.println(97);
			pw.println("and total percentage:");
			pw.println(93.5);
			System.out.println("great");
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			if(fw != null)
			{
				try 
				{
					fw.close();
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
			if(pw != null)
			{
				pw.close();
			}
		}
		
		

	}

}



Reading and writing data from text file

import java.io.*;
class ReadWrite
{
 public static void main(String args[])
 {
  try
  {
   FileReader fr=new FileReader("D:\\program\\io\\MyTextFile.txt");
   BufferedReader br=new BufferedReader(fr);
   FileWriter fw=new FileWriter("D:\\program\\io\\MyTextFile1.txt");
   PrintWriter pw=new PrintWriter(fw);
   String oneline=br.readLine();
   while(oneline!=null)
             {
              pw.println(oneline);
              oneline=br.readLine();
             }
   System.out.println("writing done");
   pw.flush();
  }
  catch(IOException e)
  {
	  e.printStackTrace();
  }
 }

}


Reading and writing data from Binary file

We use input stream and output stream classes to read and write binary data.

import java.io.*;

public class ReadWriteBinaryFile
{
	public static void main(String[] args) {
		File f=null;
		FileInputStream fis=null;
		BufferedInputStream bis = null;
		FileOutputStream fos=null;
		BufferedOutputStream bos = null;
		try {
			f=new File("Trainer.jpg");
			fis = new FileInputStream(f);
			bis =new BufferedInputStream(fis);
			byte b[]=new byte[(int)f.length()];
            bis.read(b);
			
			fos = new FileOutputStream("Trainer1.jpg");
			bos =new BufferedOutputStream(fos);
            bos.write(b);
         	
		} catch (FileNotFoundException e) 
		{
			
			e.printStackTrace();
		} 
		catch (IOException e) {
		
			e.printStackTrace();
		} 
		finally
		{
			
			try {
				if(bis != null)
					bis.close();
				
			} 
			catch (IOException e) 
			{
				
				e.printStackTrace();
			}
			try {
				if(bos != null)
					bos.close();
				
			} 
			catch (IOException e) 
			{
				
				e.printStackTrace();
			}
			
		}
		
		
	}

}



Reading data from Console

Console

An object of Console class can be used to read the string from console directly. How to get object of Console class: Console c=System.console();

import java.io.*;
class V3
{
 public static void main(String args[])
 {
  try
  {
  Console c= System.console();
  String s1=c.readLine("enter 1st no.");
  String s2=c.readLine(" enter 2nd no.");
  
  int i=Integer.parseInt(s1);
  int j=Integer.parseInt(s2);
  
  System.out.println(i/j);
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}


Footer with Map

Top