File: iotest.java

package info (click to toggle)
jswat2 2.37-1
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 7,092 kB
  • ctags: 5,592
  • sloc: java: 43,576; xml: 1,086; sh: 66; makefile: 57
file content (89 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// An input/output test for JSwat.
// $Id: iotest.java 1100 2003-12-17 00:51:48Z nfiedler $

import java.io.*;

/**
 * Class iotest tests I/O related stuff.
 *
 * @author  Nathan Fiedler
 */
public class iotest {

    /**
     * Opens the named file, then closes it. Waits for the user
     * before returning.
     *
     * @param  f  file to open.
     */
    protected static void openFileTest(String f) {
        try {
            File file = new File(f);
            System.out.println("Opening file " + file.getCanonicalPath());
            FileInputStream fis = new FileInputStream(file);
            int i = fis.read();
            System.out.println("Read one byte from file.");
            fis.close();
            System.out.println("Closed the file input stream.");
            System.out.println("Press Enter to continue...");
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            br.readLine();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    } // openFileTest

    /**
     * As the user for their name and then print it out a number of times.
     */
    protected static void inputTest() {
        System.out.print("Enter ");
        System.out.print("your ");
        System.out.print("name: ");
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        try {
            String name = br.readLine();
            for (int i = 0; i < 10; i++) {
                System.out.println(i + ": Hello " + name + '!');
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    } // inputTest

    /**
     * Displays the various 'path' values from File.
     */
    public static void filepaths() {
        File f = new File(".", "file.java");
        // Using new File("./file.java") works exactly the same.
        System.out.println("getName(): " + f.getName());
        // file.java
        System.out.println("getPath(): " + f.getPath());
        // ./file.java
        System.out.println("getAbsolutePath(): " + f.getAbsolutePath());
        // /home/me/java/./file.java
        try {
            System.out.println("getCanonicalPath(): " + f.getCanonicalPath());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        // /home/me/java/file.java
        System.out.println("getParent(): " + f.getParent());
        // .
    }

    /**
     * Tests both input and output.
     */
    public static void main(String[] args) {
        if (args.length > 0) {
            openFileTest(args[0]);
        } else {
            inputTest();
        }
        filepaths();
    } // main
} // iotest