Java by API/java.io/FileReader
Версия от 17:43, 31 мая 2010; (обсуждение)
FileReader: read()
/*
* Output:
*/
import java.io.FileReader;
public class MainClass {
public static void main(String args[]) {
try {
int counts[] = new int[10];
FileReader fr = new FileReader(args[0]);
int i;
while ((i = fr.read()) != -1) {
char c = (char) i;
int k = c - "0";
if (k >= 0 && k < 10)
++counts[k];
}
// Display digit counts
for (int j = 0; j < 10; j++) {
char c = (char) ("0" + j);
System.out.print(c + "=");
System.out.print(counts[j] + "; ");
}
fr.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
FileReader: read(char[] cbuf)
import java.io.FileReader;
public class Main {
public static void main(String[] argv) throws Exception {
FileReader fr = new FileReader("text.txt");
int count;
char chrs[] = new char[80];
do {
count = fr.read(chrs);
for (int i = 0; i < count; i++)
System.out.print(chrs[i]);
} while (count != -1);
}
}
new FileReader(String name)
/*
* Output:
*/
import java.io.FileReader;
public class MainClass {
public static void main(String args[]) {
try {
int counts[] = new int[10];
FileReader fr = new FileReader(args[0]);
int i;
while ((i = fr.read()) != -1) {
char c = (char) i;
int k = c - "0";
if (k >= 0 && k < 10)
++counts[k];
}
// Display digit counts
for (int j = 0; j < 10; j++) {
char c = (char) ("0" + j);
System.out.print(c + "=");
System.out.print(counts[j] + "; ");
}
fr.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}