In: Computer Science
What method is used to set up a file for reading and/or writing?
Note :
I write this for java language.
stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
==> 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.
InputStream f = new FileInputStream("C:/java/hello");
==> 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.
OutputStream f = new FileOutputStream("C:/java/hello")
Create a
File
To create a file in Java, you can use the
createNewFile() method. This method returns a
boolean value: true if file is successfully create, and false if
the file already exists.
Note that the method is enclosed in a try...catch block. This is
necessary because it throws an IOException if an error occurs:
code to create file:
import java.io.File; // Import the File class
import java.io.IOException; // Import the IOException class to
handle errors
public class CreateFile
{
public static void main(String[] args)
{
try
{
File myObj = new File("filename.txt");
if (myObj.createNewFile())
{
System.out.println("File created successfully : " +
myObj.getName());
} else
{
System.out.println("File already exists in system .");
}
}
catch (IOException e)
{
System.out.println("An error occurred .");
e.printStackTrace();
}
}
}
Write To a
File
FileWriter class together with its write() method to write some
text to the file we created in the example above.
Note that when you are done writing to the file, you should close
it with the close() method:
code:
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFile
{
public static void main(String[] args)
{
try
{
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("My name is Bob , i am engineer");
myWriter.close();
System.out.println("Successfully wrote to the file.");
}
catch (IOException e)
{
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Read a
File
The Scanner class to read the contents of the text file.
In modern Java applications you typically use the java.nio.file API to read and write files.
Java will read all input as a stream of bytes. The InputStream class is the superclass of all classes representing an input stream of bytes
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
// somewhere in your code
String content = new
String(Files.readAllBytes(Paths.get(fileName)));
To read a text file line by line into a List of type String
structure you can use the Files.readAllLines method.
List<String> lines = Files.readAllLines(Paths.get(fileName));
code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile
{
public static void main(String[] args)
{
try
{
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine())
{
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
}
catch (FileNotFoundException e)
{
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}