数据的读入操作(FireWrite)Read的重载方法

    @Test
    //使用read的重载方法
    public void TestFileRead1() throws IOException {
        FileReader fr = null;
        try {
            //1:File的实例化
            File file = new File("C:\\Users\\86152\\Desktop\\垃圾\\Test.txt");

            //2:FireReader的实例化
            fr = new FileReader(file);
            //3:读入的操作
            // read(char[] cbuf)--> 返回每次读入cbuf数组中的字符的个数
            //如果到末尾 返回-1
            char[] cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf)) != -1){
                for (int i = 0;i < len;i++){
                    System.out.print(cbuf[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fr.close(); //4:资源的关闭
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中有几种方法可以读取文件内容并存储到字符串变量中。其中一种方法是使用TextReader类的ReadReadLine方法。TextReader.Read方法可以将字符读取到字符缓冲区中,并指定要读取的字符数。它返回一个int值,如果已经到达读者的末尾,将返回-1。TextReader.ReadLine方法将整行读取为字符串,不包括行终止符。在.NET中,您可以使用writer.WriteLine方法将一行文本写入文件。另外,您可以使用Console.ReadLine方法从控制台读取一行文本。如果您想读取单个字符,可以使用Console.Read方法。请注意,Console.ReadLine返回的是字符串,而Console.Read返回的是int值(Unicode代码点或-1表示数据结束)。如果您想从键盘读取一个整数,请使用类似的代码: ``` string line = Console.ReadLine(); int value; if (int.TryParse(line, out value)) { Console.WriteLine("Successfully parsed value: {0}", value); } else { Console.WriteLine("Invalid number - try again!"); } ``` 还有一种方法是手动编写代码来实现读取整数的操作。下面是一个示例代码: ``` using System; namespace ConsoleCSharpLearning { class readwell { public static int readint() { int n = 0, f = 1; char ch = (char)Console.Read(); while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = (char)Console.Read(); } while (ch >= '0' && ch <= '9') { n = n * 10 + (ch - 48); ch = (char)Console.Read(); } return n * f; } } internal class Program { static void Main(string[] args) { int i = readwell.readint(); Console.ReadKey(); } } } ``` 另一种方法是使用Convert.ToInt32(Console.ReadLine())将字符串转换为整数。例如: ``` int num; num = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(num); ``` 这些是几种在C#中读取整数的方法。你可以根据你的具体需求选择适合你的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小颜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值