天天看点

某校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);
        }
    }