Java中使用字符输入流读取记事本里的内容及构造方法的举例、使用String类构造方法

创建流对象的时候,必须传入一个文件路径 类似于FileInputStream

构造举例:

①/使用文件名创建流对象
FileReader fr = new FileReader("F:\\develop\\h.txt");
②File file = new File("F:\\develop\\h.txt");
FileReader fr = new FileReader(file);

字符输入流的使用步骤

1、创建FileReader对象,构造方法中绑定要读取的数据源

2、使用FileReader对象中的方法read方法读取文件

3、关闭流.释放资源

package com.baidu.字节输入输出流;

import java.io.FileReader;
import java.io.IOException;

public class Reader01 {
    public static void main(String[] args) throws IOException {
        //
        FileReader fr = new FileReader("F:\\develop\\h.txt");
        int len = 0;
        while ((len = fr.read())!=-1){
//            System.out.println(len);//这是转换成int类型的整数了
            //接下来进行强转
            System.out.println((char)len);
        }
        fr.close();
    }
}

程序执行结果:(不想用System.out.println可以使用System.out.print这里仅仅是要表示的更直观一点)


菠
萝
吹
雪
大
大
a
b
c
1
2
3

不管文本文件里的内容是中文还是英文还是数字,都让它们以字符的形式直接打印,不存在字节流时候的乱码问题了

使用数组缓冲读取多个字符

 

 

String​(char[] value)

分配一个新的 String以便它表示当前包含在字符数组参数中的字符序列。

String​(char[] value, int offset, int count)

分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。

package com.baidu.字节输入输出流;



import java.io.FileReader;
import java.io.IOException;

public class Reader01 {
    public static void main(String[] args) throws IOException {
        //
        FileReader fr = new FileReader("F:\\develop\\h.txt");
        /*int len = 0;
        while ((len = fr.read())!=-1){
//            System.out.println(len);//这是转换成int类型的整数了
            //接下来进行强转
            System.out.println((char)len);
        }*/
        char[] cs = new char[1024];//存储读取到的多个字符
        int len =0;//记录的是每次读取的有效字符个数
        while ((len=fr.read(cs))!=-1){
            /*第一个把字符数组转换为字符串,第二个是一部分转换为字符串
            String​(char[] value) 分配一个新的 String以便它表示当前包含在字符数组参数中的字符序列。
            String​(char[] value, int offset, int count) 分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。
            offset:表示数组开始索引 count转换的个数
            */
            System.out.println(new String(cs,1,len));
        }




        fr.close();
    }
}

 

 

java写的记事本,1000行代码,基本上所有的功能都全了(和微软系统自带的记事本的相似度>70%) 其中菜单新建模块的代码如下: // 菜单 // 新建(N)按钮事件监听 newTextItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (selectedFile == null && textArea.getText().equals("")) { return; } else { int btn = JOptionPane.showConfirmDialog(container, "是否保存到" + selectedFile + "?", "是否保存", JOptionPane.YES_NO_CANCEL_OPTION); if (btn == JOptionPane.CANCEL_OPTION) { return; } else if (btn == JOptionPane.YES_OPTION) { if (selectedFile == null && !textArea.getText().equals("")) { choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); BufferedWriter bw = new BufferedWriter( new FileWriter(file)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); file.createNewFile(); bw.close(); } catch (IOException e) { JOptionPane.showConfirmDialog(container, "保存文件失败!", "ERROR", JOptionPane.ERROR_MESSAGE); } } } else if (selectedFile != null) { try { BufferedWriter bw = new BufferedWriter( new FileWriter(selectedFile)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); bw.close(); } catch (IOException e) { // JOptionPane.showConfirmDialog(container, // "保存文件失败!", // "ERROR", JOptionPane.ERROR_MESSAGE); } } } } textArea.setText(""); newPage = true; selectedFile = null; textField.setText(""); } }); 如果你初学或也在写记事本,这个绝对对你有帮助,且最适合你1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值