Junior sister learning java IO: what about writing files

brief introduction

Younger martial sister has put forward a lot of strange requirements for senior brother F, to format the output, to specific code output, to locate the output by yourself, what? And burn after reading? Let's see how senior brother f takes the moves one by one.

Character output and byte output

Younger martial sister: elder martial brother F, you talked about half of your IO last time. You basically finished reading files, but you haven't talked about writing files yet. When will you give me more popular science?

Younger martial sister: Senior brother F, you know I have always been a model of diligence and studiousness, a good student in the eyes of teachers, a good example in the hearts of students, and a good child around my parents. When I was always climbing the peak of science, I found that half of my knowledge had not been obtained. It really made me sigh. Senior brother F, pass on the knowledge to me quickly.

Younger martial sister, your request, elder martial brother, I should try my best to do it, but how can I remember that it has been several days since I talked about reading IO files last time? Why did you come to me today.

Younger martial sister blushed: elder martial brother F, I don't want to review your knowledge again because I have encountered some problems when using it.

More highlights:

Then go through the structure of the output class again:

The above are the two output systems: writer and OutputStream.

Writer is mainly for characters, while stream is mainly for bytes.

The most commonly used writers are filewriter and bufferedwriter. Let's look at the next basic write example:

public void useBufferedWriter() throws IOException {
        String content = "www.flydean.com";
        File file = new File("src/main/resources/www.flydean.com");

        FileWriter fw = new FileWriter(file);
        try(BufferedWriter bw = new BufferedWriter(fw)){
            bw.write(content);
        }
    }

Bufferedwriter is the encapsulation of filewriter. It provides a certain buffer mechanism to improve the efficiency of writing.

In fact, bufferedwriter provides three writing methods:

public void write(int c)
public void write(char cbuf[],int off,int len)
public void write(String s,int len)

The first method passes in an int, the second method passes in the character array and the position and length to start reading, and the third method passes in the string and the position and length to start reading. Is it simple and completely understandable?

Younger martial sister: No, elder martial brother F, the parameters of the last two methods, whether char or string, are characters. I can understand. What the hell is the first method passing in int?

Younger martial sister, have you forgotten what I told you before? The underlying storage of int is bytes, and the underlying storage of char and string is bytes. Let's just make a forced conversion between int and char. Let's see how it is converted:

public void write(int c) throws IOException {
        synchronized (lock) {
            ensureopen();
            if (nextChar >= nChars)
                flushBuffer();
            cb[nextChar++] = (char) c;
        }
    }

Remember how many bytes int takes? 4. Char needs 2 bytes. In this way, the forced conversion from int to char will have the problem of precision loss. Only the low-order 2-byte data will be retained, and the high-order 2-byte data will be discarded. This needs to be noted in use.

After reading writer, let's take a look at stream:

public void useFileOutputStream() throws IOException {
        String str = "www.flydean.com";
        try(FileOutputStream outputStream = new FileOutputStream("src/main/resources/www.flydean.com");
            bufferedoutputstream bufferedoutputstream= new bufferedoutputstream(outputStream)){
            byte[] strToBytes = str.getBytes();
            bufferedoutputstream.write(strToBytes);
        }
    }

Like writer, bufferedoutputstream encapsulates fileoutputstream. Let's take a look at the write method provided in bufferedoutputstream:

public synchronized void write(int b)
public synchronized void write(byte b[],int len)

Comparing with writer, bufferedoutputstream's method is synchronized, and bufferedoutputstream operates directly on byte.

The int parameter passed in by the first write method also needs to be intercepted, but this time it is converted from int to byte.

Format output

Younger martial sister: Senior brother F, we often use system out. Println can directly output formatted strings to standard output. Does file writing have similar functions?

There must be. Printwriter is used for formatted output:

public void usePrintWriter() throws IOException {
        FileWriter fileWriter = new FileWriter("src/main/resources/www.flydean.com");
        try(PrintWriter printWriter = new PrintWriter(fileWriter)){
            printWriter.print("www.flydean.com");
            printWriter.printf("程序那些事 %s ","非常棒");
        }
    }

Export other objects

Younger martial sister: elder martial brother F, we can see that string, char and byte can be output. Can we output integer, long and other basic types?

Yes, you can use dataoutputstream to:

public void useDataOutPutStream()
            throws IOException {
        String value = "www.flydean.com";
        try(FileOutputStream fos = new FileOutputStream("src/main/resources/www.flydean.com")){
            DataOutputStream outStream = new DataOutputStream(new bufferedoutputstream(fos));
            outStream.writeUTF(value);
        }
    }

Dataoutputstream provides writelong, writedouble, writefloat and other methods, as well as writeutf!

Write at a specific location

Younger martial sister: Senior brother F, sometimes we don't need to write to the file from scratch every time. Can we customize where to write?

You can use RandomAccessFile:

public void useRandomAccess() throws IOException {
        try(RandomAccessFile writer = new RandomAccessFile("src/main/resources/www.flydean.com","rw")){
            writer.seek(100);
            writer.writeInt(50);
        }
    }

RandomAccessFile can be located through seek, and then written from the specified location through the write method.

Lock the file

Younger martial sister: brother F, finally, there is another question. How can I ensure that when I write a file, others will not overwrite what I write and there will be no conflict?

Filechannel can call the trylock method to obtain a filelock lock. Through this lock, we can control file access.

public void useFileLock()
            throws IOException {
        try(RandomAccessFile stream = new RandomAccessFile("src/main/resources/www.flydean.com","rw");
        FileChannel channel = stream.getChannel()){
            FileLock lock = null;
            try {
                lock = channel.tryLock();
            } catch (final OverlappingFileLockException e) {
                stream.close();
                channel.close();
            }
            stream.writeChars("www.flydean.com");
            lock.release();
        }
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>