某校2020专硕编程题-读取文件

题目

读取文件1.txt,每行不超过100个字符,找到每行中最长的单词,输出单词长度
单词的文本应该是多行

Java实现

题目实现

public static void test04() throws IOException {
        InputStream is = new FileInputStream("1.txt");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < is.available(); i++) {
            sb.append((char) is.read());
        }
        String s = sb.toString();
        String[] line = s.split("\n");
        for (int i = 0; i < line.length; i++) {
            String[] word = line[i].split(" ");
            int length = 0;
            for (int j = 0; j < word.length; j++) {
                if (word[j].length() > length ) {
                    length = word[j].length();
                }
            }
            System.out.println("line:"+i+",length:"+length);
        }
}

如果没有文件可以自己创建,代码如下

public static void create() throws IOException {
        OutputStream os = new FileOutputStream("1.txt");
        String a = "This paper is intended to explore the meaningfulness of interdisciplinary approaches to cultural studies, identities, and semiotics in the field of language education and applied linguistics in Korea. The ‘cultural turn’, which notes representation issues in mass culture, identity construction, and semiotics, are reviewed and intertwined to lead to the question of how high-stakes test takers are described in the mass media. \n10 printed advertisements in which test takers are modeled to prepare for NEAT (National English Ability Test) and TOEIC \n(Test of English for International Communication) were analyzed through syntagmatic and paradigmatic analysis, as well as through the Barthes’ signification analysis. Results obtained from semiotic analysis revealed that NEAT and TOEIC learners were often described in a stereotypical manner, positioned as taken-for-granted, competitive, emotional test-takers in conventional teach-to-the-test culture. \nThe (distorted) representation of test takers discussed in this study, however, needs to be researched in different media settings, since the values of test prep appeared multiple, conflicting, and even entertaining in the present study, especially in the recent coverages. Meanings surrounding TOEIC learners, for example, could be interpreted as both forced and voluntary, competitive and friendly. It was found that the research tradition built on cultural studies, along with semiotic approaches, has great potential to critically understand a better meaning-making signification in the field of language education industry.";
        os.write(a.getBytes());
        os.close();
}

IO流相关练习

  1. 使用流从控制台输入字符并打印,输入q退出
    public static void IO1() throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        char c;
        while (true){
            c = (char) r.read();
            if (c == 'q') break;
            System.out.print(c);
        }
    }
  1. 读取和显示字符行直到你输入了单词"end"。
    public static void IO2() throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while (true){
            str = r.readLine();
            if ("end".equals(str)) break;
            System.out.println(str);
        }
    }
  1. 创建一个文件写入"hello world" 并读取文件打印到控制台
    public static void IO3() throws IOException {
        OutputStream os = new FileOutputStream("test");
        String str = "hello world";
        os.write(str.getBytes());
        os.close();
        InputStream is = new FileInputStream("test");
        while (true){
            int c = is.read();
            if (c == -1) break;
            System.out.print((char)c);
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

图图是只猫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值