Showing posts with label IO / Input Output. Show all posts
Showing posts with label IO / Input Output. Show all posts

Tuesday, May 17, 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:
/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, 
length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, 
offset, length);
int ch;

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

}



Using the above function:

String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

Saturday, May 14, 2011

Classes helping in parsing the input

Some of the classes in the Java IO API are designed to help you parse input. These classes are:
  • PusbackInputStream
  • StreamTokenizer
  • PushbackReader
  • LineNumberReader
It is not the purpose of this text to give you a complete course in parsing of data. The purpose was rather to give you above quick list of classes related to parsing of input data.
If you have to parse data you will often end up writing your own classes that use some of the classes in this list. I know I did when I wrote the parser for the Butterfly Container Script. I used the PushbackInputStream at the core of my parser, because sometimes I needed to read ahead a character or two, to determine what the character at hand meant.

Thursday, May 12, 2011

Read partial byte array using ByteArrayInputStream Example

We can write byte array completely as well as partially, i.e. we can write byte array from one index to other. We have already written byte array completely here. We can read byte array partially like this:

/* uses ByteArrayInputStream(byte[] b, int offset, int length)
*/
public static void printPartialByteArray(byte[] bytes, offset, length)
ByteArrayInputStream bis = new ByteArrayInputStream(bytes, offset, length);
int ch;

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

}




Using the above function:



String str = "Partial Byte Array InputStream Example";

//get bytes from string
byte[] bytes = str.getBytes();
printPartialByteArray(bytes,5,5);

Read byte array using ByteArrayInputStream Example

Here we read the byte array and put it on console.

Function to read the byte array:

public static void readByteArray(byte[] bytes){
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);

int ch;

//read bytes from ByteArrayInputStream using read method
while((ch = bai.read()) != -1)
{
System.out.print((char)ch);
}


}



Using the above function:



String str = "ByteArrayInputStream Example!";

//get bytes from string using getBytes method
byte[] bytes = str.getBytes();
readByteArray(bytes);



For partial reading of byte array read here.

Wednesday, May 11, 2011

Write byte array to file using BufferedOutputStream

Writing the function:

public static void writeByteArrayToFile(String fileName, byte[] byteArr)
{
BufferedOutputStream bos = null;
try
{
//create an object of FileOutputStream
FileOutputStream fos = new FileOutputStream(new File(strFileName));
//create an object of BufferedOutputStream
bos = new BufferedOutputStream(fos);


/*
* To write byte array to file use,
* public void write(byte[] b) method of BufferedOutputStream
* class.
*/
System.out.println("Writing byte array to file");
bos.write(byteArr);
System.out.println("File written");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing file" + ioe);
}
finally
{
if(bos != null){
try
{
//flush the BufferedOutputStream
bos.flush();
//close the BufferedOutputStream
bos.close();
}
catch(Exception e){//don't swallow
//exceptions..add your code accordingly
}
}
}
}


Using the above function:


String fileName = "C:\demo.txt";
String str = "BufferedOutputStream Example to write byte array";
writeByteArrayToFile(fileName,str.getBytes());

Write byte to file using BufferedOutputStream

 

public static void writeByte2File(byte b, String fileName)
{
BufferedOutputStream bos = null;

try
{
//Create FileOutputStream for the file
FileOutputStream fos = new FileOutputStream(fileName);
//Create BufferedOutputStream object
//for FileOutputStream

bos = new BufferedOutputStream(fos);

/*
* To write a byte to BufferedOutputStream use,
* public void write(int b) method of BufferedOutputStream
* class.
*
* This method may throw IOException if something goes
* wrong.
*/

System.out.println("BufferedOutputStream: writing file");

bos.write(b);

System.out.println("BufferedOutputStream : written file");
}
catch(FileNotFoundException fnfe)
{
System.out.println("Specified file not found" + fnfe);
}
catch(IOException ioe)
{
System.out.println("Error while writing to file" + ioe);
}
finally
{
try
{
if(bos != null)
{
bos.flush();
bos.close();
}

}
catch(Exception e){}

}
}



Eg. of calling function:


String fileName="C:\demo.txt";
byte b = 65;
//this will write a character represented by ascii 65
//i.e. 'A'
writeByte2File(b,fileName);

Read File in String Using Java BufferedInputStream Example

public static String readFile(String fileName){
File file = new File(fileName);
BufferedInputStream bin = null;

try
{
//create FileInputStream object
FileInputStream fin = new FileInputStream(file);

//create object of BufferedInputStream
bin = new BufferedInputStream(fin);

//create a byte array
byte[] contents = new byte[1024];

int bytesRead=0;
String strFileContents;
StringBuffer buf= new StringBuffer();

while( (bytesRead = bin.read(contents)) != -1){

strFileContents = new String(contents, 0, bytesRead);
buf.append(strFileContents)
System.out.print(strFileContents);
}
return buf.toString();
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file "
+ ioe);
}
finally
{
//close the BufferedInputStream using close method
try{
if(bin != null)
bin.close();
return String.Empty();
}catch(IOException ioe)
{
System.out.println("Error while closing the stream :"
+ ioe);
return String.Empty();
}

}
}


File name can be "C://FileIO//ReadFile.txt".

Moving file or directory from one directory to another

Here we write function to copy file or directory from one place to other.

public static boolean moveFileOrDirectory(String src, String dst){
File source = new File(src);
if(!source.exists())
return false;


File destination = new File(dest);
if(!destination.exists()){
System.out.print("Mentioned directory does not exist.
\nDo you want to create a new directory(Y/N)? "
);

String chk = readString();
if(chk.equals("Y") || chk.equals("y")){
destination.mkdir();
return copyDirectory(source, destination);
}else{
return false;
}
}else //if file or directory already exists, we have to copy and
//overwrite the already existing one
{
System.out.println("Given file or folder name already exists.
\nDo you want to replace now?"
);
String chk = readString();
if("Y".equals(chk) )
return copyDirectory(source, destination);
else return false;
}
}



 


Now this function makes use of copyDirectory function which can be written like this:



public static boolean copyDirectory(File sourceDir, File destDir) throws
IOException{

if(!destDir.exists()){
destDir.mkdir();
}

File[] children = sourceDir.listFiles();
for(File sourceChild : children){

String name = sourceChild.getName();

File destChild = new File(destDir, name);

if(sourceChild.isDirectory()){

return copyDirectory(sourceChild, destChild);

}

else{

return copyFile(sourceChild, destChild);

}

}

}



Now this in turn makes use of copyFile function.



public static void copyFile(File source, File dest) throws IOException{

if(!dest.exists()){

dest.createNewFile();

}

InputStream in = null;

OutputStream out = null;

try{

in = new FileInputStream(source);

out = new FileOutputStream(dest);

byte[] buf = new byte[1024];

int len;

while((len = in.read(buf)) > 0){

out.write(buf, 0, len);

}

}

finally{

in.close();

out.close();

}
return true;

}

Tuesday, May 10, 2011

Copy multiple files (java)

We have already seen the function to copy 1 file to another destination. Here we have to write function to copy multiple file to 1 location.

Refer this article for copyFile() function.

public static boolean copyMultipleFiles(String dst, String ... src){
for(String oneSrc : src)
{
boolean isCopied = copyFile(oneSrc,dst);
if(!isCopied)
return isCopied;
//Note that i have not yet figured out the case of rollback in case of
//unsuccessful copy of 1 file. Because this depends on your application.
//So if you want to rule out any copy if even 1 copy file fails,
//just call the delete function and so on.
}
return true;
}



Also note that this method uses varargs function, but you can use array instead.

Java - Copying one file to another

Here is function called copyFile  which takes 2 strings – source and destination. It copies source to destination.

 

private static void copyFile(String srcFile, String dstFile){

try{

File f1 = new File(srcFile);

File f2 = new File(dstFile);

InputStream in = new FileInputStream(f1);

//Append the file
OutputStream out = new FileOutputStream(f2,true);
//Overwrite the file.
OutputStream out = new FileOutputStream(f2);


byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0){

out.write(buf, 0, len);

}

in.close();

out.close();

System.out.println("File copied.");

}

catch(FileNotFoundException ex){

System.out.println(ex.getMessage() + " in
the specified directory."
);
return false;

}

catch(IOException e){

System.out.println(e.getMessage());
return false;

}
return true;
}

Rename the File or Directory in java

This example shows function, which renames the file or directory. The function takes old name and new name, and renames accordingly.

public boolean renameFile(String oldName, String newName)
{
File oldfile = new File(oldName);
if(!oldfile.exists())
{
System.out.println("File or directory does not exist.");
return false;
}
File newfile = new File(newName);
boolean reName = oldfile.renameTo(newfile);
if(!reName )
{
System.out.println("File or directory does not rename successfully.");
return reName ;
}else{
System.out.println("File or directory rename is successfully.");
return reName;
}
}

Append to file in java

See this article to see about FileWriter and BufferedWriter in java. We have already seen how to write to file in java. Appending is on the same lines, just adding true to FileWriter object.

FileWriter fstream = new FileWriter("out.txt",true);



Full example:


public static void appendToFile(String fileName)
{
try{
//Create file
FileWriter fstream = new FileWriter(fileName,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello I just wanted to append");
//Close the output stream
out.close();
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}

Java Write To File using BufferedWriter

For writing data to a file, the class FileWriter and BufferedWriter are used.

FileWriter :

FileWriter is a subclass of OutputStreamWriter class that is used to write text (as opposed to binary data) to a file. You create a FileWriter by specifying the file to be written to, or optionally, when the data should be appended to the end of an existing file instead of overwriting that file. The FileWriter class creates an internal FileOutputStream to write bytes to the specified file

BufferedWriter :

The BufferedWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays and strings.
The constructor of the FileWriter class takes the file name which has to be buffered by the BufferedWriter stream. The write( ) method of BufferedWriter class is used to create the file into specified directory.

Here again decorator pattern is used, where FileWriter object is decorated to BufferedWriter to carry out write operation.

public static String readString()
{
BufferedReader in ;
String str = String.Empty();
try{
in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter string to be input: ");
str = in.readLine();
}
return str;
}

public static boolean writeFile(String fileName)
{
boolean exist = file.createNewFile();
if (!exist)
{
System.out.println("File already exists.");
return exist;

}
else
{
FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);
out.write(readString());
out.close();
System.out.println("File created successfully.");
return exist;

}
}



See also how to append text file.

Java: read a text file line by line

This class reads a text file line by line and echos the contents to standard out.
Here we have used BufferedReader to do this.
import java.io.*;

public static String readFile(String fileName)
// nested initialization,
// or in design pattern jargon, decorate FileInputStream with
// BufferedInputStream.
BufferedReader in = new BufferedReader(new FileReader(fileName));
try {
// read file line by line
String line = in.readLine();
while (line != null) {
printLine(line);
line = in.readLine();
}
}
finally {
// always close input stream
in.close();
}
}
private static void printLine(String line) {
System.out.println(line);
}

Create a File

For this createNewFile() method is used. It returns true if file is created and false otherwise. So for eg. it returns false if file already exist, so it can't create the new file here.

import java.io.*;
//now wherever you want do this:
public static void main(String[] args){
File file = new File("C://demo.txt");
boolean isCreated;
try
{
isCreated= file.createNewFile();
}
catch(IOException ioe)
{
System.out.println("Error in creating a new empty file :" + ioe);
}
}

Working with Directory tutorial

Following topics can be covered in this tutorial

Standard Streams in java

Standard Streams are a feature provided by many operating systems. By default, they read input from the keyboard and write output to the display. They also support I/O operations on files.

Java also supports three Standard Streams:

  • Standard Input: Accessed through System.in which is used to read input from the keyboard.
  • Standard Output: Accessed through System.out which is used to write output to be display.
  • Standard Error: Accessed through System.err which is used to write error output to be display.

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.
System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader inp = new InputStreamReader(system.in);

Using DataInputStream and DataOutputStream


Serialization tutorial index


How to Concatenate Files using SequenceInputStream

The SequenceInputStream(in the API reference documentation) creates a single input stream from multiple input sources. This example program uses SequenceInputStream to implement a concatenation utility that sequentially concatenates files together in the order they are listed on the command line.

So lets first create the Utility class which perform this application, say we call it ListOfFiles which takes list of file names in its constructor, and then concatenates them by creating a new InputStream for every filename listed as argument.

Creating the Utility Class

import java.util.*;
import java.io.*;

public class ListOfFiles implements Enumeration {

private String[] listOfFiles;
private int current = 0;

public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}

public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}

public Object nextElement() {
InputStream in = null;

if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can't open " + nextElement);
}
}
return in;
}
}


ListOfFiles implements the Enumeration(in the API reference documentation) interface. You'll see how this comes into play as we walk through the rest of the program.


Using this Utility class


import java.io.*;

public class Concatenate {
public static void main(String[] args) throws IOException {
ListOfFiles mylist = new ListOfFiles(args);

SequenceInputStream s = new SequenceInputStream(mylist);
int c;

while ((c = s.read()) != -1)
System.out.write(c);

s.close();
}
}


After the main method creates the SequenceInputStream, it reads from it one byte at a time. When the SequenceInputStream needs an InputStream from a new source (such as for the first byte read or when it runs off the end of the current input stream), it calls nextElement on the Enumeration object to get the next InputStream. ListOfFiles creates FileInputStream objects lazily, meaning that whenever SequenceInputStream calls nextElement, ListOfFiles opens a FileInputStream on the next filename in the list and returns the stream. When the ListOfFiles runs out of files to read (it has no more elements), nextElement returns null, and the call to SequenceInputStream's read method returns -1 to indicate the end of input.

Concatenate simply echos all the data read from the SequenceInputStream to the standard output.

Chitika