天天看點

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