Wednesday, 27 September 2017

Java Files

Create a New File if it not exitst and write content into file

import java.io.*;

public class WriteIntoFile {

public static void main(String args[]) throws IOException{

  File f =new File("log.txt");
       
           if(!f.exists())
           f.createNewFile();
           System.out.println(f.getAbsolutePath());

       
         FileOutputStream fout = new FileOutputStream(f);
fout.write("Writing into file".getBytes());

fout.flush();
fout.close();

       
       
         System.out.println("success");
}
}



--------------------------------------------------------------------------------------------------------------

  Reading from file and writing into Console output



import java.io.*;

public class ReadingFromFile {

public static void main(String args[]) throws IOException{

      FileInputStream in = new FileInputStream("log.txt");
 
        int c;
        while((c=in.read())!=-1){
        System.out.print((char)c);
        }

}

}



--------------------------------------------------------------------------------------------------------------

Write and Read from File using  FileInputStream  &  FileOutputStream



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class ReadingWriting {

public static void main(String args[]) throws IOException{

Scanner sc = new Scanner(System.in);
System.out.print("enter input: ");
String str= sc.nextLine();
System.out.println(str);

FileOutputStream fout=new FileOutputStream("Output.txt");
//System.out.println(str.getBytes());

fout.write(str.getBytes());

fout.flush();
fout.close();


FileInputStream fin=new FileInputStream("Output.txt");
int c;
while((c=fin.read())!=-1){
System.out.print((char)c);
}

fin.close();
}
}


//enter input: a b c
//a b c
//9732983299                   output byte
// System.out.print((char)c);  output chars



--------------------------------------------------------------------------------------------------------------


 Reading & Writing into files using FileReader and FileWriter

import java.util.Scanner;
import java.io.*;
public class FileReaderWriter {

public static void main(String args[]) throws IOException{

Scanner sc = new Scanner(System.in);

System.out.print("enter input: ");
String str = sc.nextLine();

FileWriter fout = new FileWriter("fout.txt");


fout.write(str);

fout.flush();
fout.close();

int c;

FileReader fin = new FileReader("fout.txt");

while((c=fin.read())!=-1){
System.out.print((char)c);
}


fin.close();
}
}

--------------------------------------------------------------------------------------------------------------

InputStream & OutputStream 


import java.io.*;

public class InputOutputStream {

public static void main(String args[]) throws IOException{

OutputStream out=new FileOutputStream("file.txt");

byte bArr[]={65,32,66,32,67};

for(int i=0;i<bArr.length;i++){
out.write(bArr[i]);
}
out.flush();
out.close();



InputStream in = new FileInputStream("file.txt");

int size=in.available();
System.out.println(size);

for(int j=0;j<size;j++){
System.out.print((char)in.read());
}

in.close();

}

}


// 5
// A B C

--------------------------------------------------------------------------------------------------------------

Create a new Directory

package com.files;

import java.io.*;
public class CreateDirectory {

public static void main(String args[]){

String dirname ="Z:/Java/Files/tmp";
File f = new File(dirname);

boolean check;
check=f.mkdir();
System.out.println(check);     // return true if directory is created
}
}
--------------------------------------------------------------------------------------------------------------

List Directories


import java.io.*;

public class ListDirectories {

public static void main(String args[]){

File file =null;
String[] paths;


file =new File("Z:/Java/Files");

  paths=file.list();
 
  for(String path:paths){
  System.out.println(path);
  }
}
}

--------------------------------------------------------------------------------------------------------------






--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

http://tutorials.jenkov.com
Notes

FileInputStream & FileOutputStream 

-   Both are Byte Stream

-   Reads and writes 8 bits at a time

-  FileOutputStream can be used create a new file if not exist

-  File f =new File("log.txt");     It won't create a new file


FileReader  & FileWriter

- Both are character streams

-  Reads and  writes 16 bit unicode

-  Reads and writes 2 Bytes at a time

- Internally uses FileInputStream and FileOutputStream

-  FileWriter can be used to create a new file if not exist









FileInputStream

This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file −
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows −
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Once you have InputStream object in hand, then there is a list of helper methods which can be used to read to stream or to do other operations on the stream.

FileOutputStream

FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object to write the file −
OutputStream f = new FileOutputStream("C:/java/hello") 
Following constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method as follows −
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Once you have OutputStream object in hand, then there is a list of helper methods, which can be used to write to stream or to do other operations on the stream.








No comments:

Post a Comment