Home>

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

The first thing you should think about when answering this question is that java is just a language,One of our use tools,So the answer is clear,Is to write all kinds of external data into a certain file,For preservationOr read its data from a file,For our use.Just like the movie process,Download a movie from a network resource and save it on your computer (write a file). When i want to watch it, open it with a player (read a file).

How to read and write files in java?

Do it first,There are two types of streams in java.Byte and character streams,The two base classes of byte stream are inputstream and outputstream;the two base classes of character stream are reader and writer. The so-called file stream,That is, we cannot leave the stream open for file operations.From this we know that we need to use a class that must inherit one of the four base classes described above.Everything in java is a class,Everything is an object.Naturally, I think of what kinds of file operations are:

The following four classes are used directly:

Byte stream:fileinputstream and fileoutputstream

Character stream:filereader and filewriter

Just find the class.The rest is to find the implementation method.

Two options are here,This involves how do we choose the appropriate file reading and writing method?

The difference between the selection conditions:

Read the file in bytes,Commonly used to read binary files,Such as pictures, sounds, images and other files.

Read the file in characters,Often used to read text,Number and other types of files.

As for whether you choose to use a buffer to encapsulate the file input and output streams,Depends on the size of the file,For large files,The buffer bucket is selected to provide file read and write efficiency.

The following is a simple application example:

1. Directly read and write files using byte streams:

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! \ r \ n";
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. Use character streams to directly read and write files:

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! \ r \ n");
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 and write streams after buffer encapsulation:

1. Read and write the file after encapsulating the byte stream:

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 ();
}
}
}
  • Previous Tutorial on using UPDATE and DELETE statements in MySQL
  • Next Explain the internal implementation principle of Handler in Android
  • Trends