• 周五. 2月 23rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

021.5 IO流——字符流

admin

11月 28, 2021

###############基本功能

FileWriter fw = new FileWriter("xxx.txt");
fw.write("哈喽");
fw.close();

FileReader fr = new FileReader("xxx.txt");
int ch = 0;
while((ch = fr.read()) != -1){
    System.out.println((char)ch);
}
fr.close();

###############字符流缓冲区
写入

private static void writeTextByBuffered() throws IOException
{
    FileWriter fw = new FileWriter("char_xxx.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("小红");
    bw.newLine();
    bw.write("小花,小华");
    bw.flush();
    bw.close();
}

读取

private static void writeTextByBuffered() throws IOException
{
    FileWriter fw = new FileWriter("char_xxx.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("小红");
    bw.newLine();
    bw.write("小花,小华");
    bw.flush();
    bw.close();
}

读取键盘录入信息,Scanner=流+正则表达式。方法是按照某种规则在读取数据
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in);
bufr.readLine();

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注