Java programming file reading and writing examples

  • 2020-04-01 04:30:16
  • OfStack

This article illustrates how to read and write files in Java programming. Share with you for your reference, as follows:

What is the role of file read and write operations in Java?

When answering this question, we should first think that Java is just a language, we use a tool, so the answer is clear, is to write foreign data into a file to save; Or read the data out of the file for us to use. For the movie process, download a movie from a web source and save it on your computer (write the file), then open it on the player when you want to watch it (read the file).

How do I read and write to a file in Java?

For the sake of argument, there are two types of streams in Java, byte stream and character stream. The two base classes of byte stream are InputStream and OutputStream. The two base classes for the character stream are Reader and Writer. The so-called file flow, that is, we do not leave open the operation of the file. So we know that we're going to use a class that necessarily inherits one of the four base classes. Everything in Java is a class, everything is an object. Naturally, you will think of the file operation classes:

Here are four directly used classes:

In byte stream: FileInputStream and FileOutputStream
In the character stream: FileReader and FileWriter

Finding the class is easy. All that's left is to find the implementation.

The two options are here, and this is where we come in. How do we choose the right way to read and write files?

Selection criteria:

Read a file in bytes, often used to read binary files, such as pictures, sounds, images, etc.
To read a file in character units, often used to read text, Numbers, and other types of files.
Whether to use Buffer to encapsulate the input and output stream of the file depends on the size of the file. For large files to be read and written, use the Buffer bucket to provide file read and write efficiency.

The following is a simple application example:

1. Directly read and write files with byte stream:

Note: FileOutputStream (file, true); Inside, the true parameter means that the original file is not overwritten, and the content is appended to the end of the file.


public class FileTest
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
FileOutputStream out = new FileOutputStream(file, true);
String s = "Hello,world!rn";
out.write(s.getBytes());
out.flush();
out.close();
//FileInputStream in = new FileInputStream(file);
//byte [] b = new byte[20];
//in.read(b, 0, b.length);
//System.out.println(new String(b));
//in.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

2. Directly read and write files by using character stream:


public class File03
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter(file,true);
fw.write("Hello,world!rn");
fw.flush();
fw.close();
//FileReader fr = new FileReader(file);
//int i=0;
//String s ="";
//while( ( i = fr.read() )!= -1)
//{
// s = s +(char)i;
//}
//System.out.println(s);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

Application of file read-write flow encapsulated by Buffer:

1. Read and write files after byte stream encapsulation:


static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
// FileOutputStream out = new FileOutputStream(file, true);
// BufferedOutputStream bout = new BufferedOutputStream(out);
// String s = "I have a dream!";
// bout.write(s.getBytes());
// bout.flush();
// bout.close();
FileInputStream in = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(in);
byte[] b = new byte[15];
bin.read(b);
bin.close();
System.out.println(new String(b));
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

2. Read and write the file after encapsulating the character stream:


public class File03
{
static File file = new File("d:/test.txt");
public static void main(String[] args)
{
try
{
// FileWriter fw = new FileWriter(file, true);
// BufferedWriter bw = new BufferedWriter(fw);
// String nextLine = System.getProperty("line.separator");
// bw.write("Hello,world!" + nextLine);
// bw.flush();
// bw.close();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int i = 0;
String s = "";
String temp = null;
while((temp=br.readLine())!=null)
{
s = s+temp;
}
System.out.println(s);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

I hope this article has been helpful to you in Java programming.


Related articles: