File: FileTest.java

package info (click to toggle)
waba 1.5-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 1,996 kB
  • ctags: 3,231
  • sloc: ansic: 17,303; java: 4,436; sh: 2,345; makefile: 417
file content (138 lines) | stat: -rw-r--r-- 3,091 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

// library inclusions
import waba.io.*;
import java.lang.*;
import waba.sys.*;
import waba.sys.VmShell;

/*
 * Todo: Test createDir, delete, exists, rename, seek
 */ 

class FileTest
{
  public static void main (String[] args) {
    VmShell.println("FileTest" + " "+args.length);
    FileTest f = new FileTest();

    if(args.length<1 || args[0]==null)
      usage();
    else {
      if(args[0].equals("a"))
	testLD();
      else if(args[0].equals("b"))
	testFRd();
      else if(args[0].equals("c"))
	testFCr();
      else
	usage();
    }
  }  // method main


  private static void usage()
  {
      VmShell.println("usage: FileTest a|b|c|d\n");
  }

  private static void testLD()
  {
    File file;
    VmShell.println("ListDir:");
    file = new File(".", File.READ_ONLY);
    if(!file.isOpen()) {
      VmShell.println("*** could not open dir");
    }
    else {
      String list[];
      list = file.listDir();
      if(list==null) {
	VmShell.println("*** could not listdir");
      }
      else {
	int l = list.length;
	VmShell.println("Found "+l+" object"+(l>1?"s":""));
	for(int i=0; i<l; ++i) {
	  File f = new File(list[i], File.READ_ONLY);

	  VmShell.print(f.isDir() ? "d ":"f ");
	  VmShell.print("[");
	  VmShell.print(Convert.toString(i));
	  VmShell.print("] = ");
	  VmShell.print(list[i]);

	  VmShell.print("\r\t\t\t\t");
	  VmShell.print(Convert.toString(f.getLength()));
	  VmShell.print("\t[");
	  VmShell.print(f.getPath());
	  VmShell.print("]");


	  VmShell.println("");
	  f.close();
	}
      }
    }
    
  }  // method

  static final String fname = "test.txt";

  private static void testFRd()
  {
    VmShell.println("Read file:");

    File file = new File(fname, File.READ_ONLY);
    if (!file.isOpen()) {
      VmShell.println("*** could not open file "+'"'+fname+'"'+" for reading");
      return;
    }
    byte b[] = new byte[25];
    int nr = file.readBytes(b, 0, b.length);
    VmShell.println("read "+nr+" byte"+(nr>1?"s":"")+":");
    VmShell.print("<");
    for(int i=0; i<nr; ++i)
      VmShell.print(""+(char)b[i]);
    VmShell.println(">");
    file.close();
  }  // method

  private static void testFCr()
  {
    VmShell.println("Create file:");

    File file = new File(fname, File.CREATE);
    if (!file.isOpen()) {
      VmShell.println("*** could not open file "+'"'+fname+'"'+" for create");
      return;
    }
    byte b[] = new byte[25];
    for(int i=0; i<b.length; ++i) b[i]=(byte)(255-i);
    int nr = file.writeBytes(b, 0, b.length);
    VmShell.println("wrote "+nr+" byte"+(nr>1?"s":"")+":");
    file.close();
  }  // method

  
}  // class HelloWorld


/*
 * File is a file or directory.
 * <p>
 * The File class will not work under the PalmPilot since it does not
 * contain a filesystem.
 * <p>
 * Here is an example showing data being read from a file:
 *
 * <pre>
 * File file = new File("/temp/tempfile", File.READ_ONLY);
 * if (!file.isOpen())
 *   return;
 * byte b[] = new byte[10];
 * file.readBytes(b, 0, 10);
 * file.close();
 * file = new File("/temp/tempfile", File.DONT_OPEN);
 * file.delete();
 * </pre>
 */