威尼斯wns.9778官网活动_vnsc威尼斯城官网

热门关键词: 威尼斯wns.9778官网活动,vnsc威尼斯城官网
当前位置:威尼斯wns.9778官网活动 > 计算机教程 > java使用BufferedOutputStream写文件

java使用BufferedOutputStream写文件

文章作者:计算机教程 上传时间:2019-05-15

下面代码演示如何使用BufferedOutputStream类写文件。

下面代码演示如何使用BufferedOutputStream类写文件。

使用BufferedOutputStream类写文件,需要先将字符串转换为字节数组,然后再写入。

使用BufferedOutputStream类写文件,需要先将字符串转换为字节数组,然后再写入。

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author outofmemory.cn
 */
public class Main {

    /**
     * Prints some data to a file
     */
    public void writeToFile(String filename) {

        BufferedOutputStream bufferedOutput = null;

        try {

            //Construct the BufferedOutputStream object
            bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));

            //Start writing to the output stream
            bufferedOutput.write("Line one".getBytes());
            bufferedOutput.write("n".getBytes()); //new line, you might want to use rn if you're on Windows
            bufferedOutput.write("Line two".getBytes());
            bufferedOutput.write("n".getBytes());

            //prints the character that has the decimal value of 65
            bufferedOutput.write(65);

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedOutputStream
            try {
                if (bufferedOutput != null) {
                    bufferedOutput.flush();
                    bufferedOutput.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().writeToFile("myFile.txt");
    }
}
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
*
* @author outofmemory.cn
*/
public class Main {

/**
* Prints some data to a file
*/
public void writeToFile(String filename) {

BufferedOutputStream bufferedOutput = null;

try {

//Construct the BufferedOutputStream object
bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));

//Start writing to the output stream
bufferedOutput.write("Line one".getBytes());
bufferedOutput.write("n".getBytes()); //new line, you might want to use rn if you're on Windows
bufferedOutput.write("Line two".getBytes());
bufferedOutput.write("n".getBytes());

//prints the character that has the decimal value of 65
bufferedOutput.write(65);

} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedOutputStream
try {
if (bufferedOutput != null) {
bufferedOutput.flush();
bufferedOutput.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().writeToFile("myFile.txt");
}
}

本文由威尼斯wns.9778官网活动发布于计算机教程,转载请注明出处:java使用BufferedOutputStream写文件

关键词: