Java by API/java.io/FileReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 17:32, 31 мая 2010
FileReader: read()
<source lang="java">
/*
* 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); } }
}
</source>
FileReader: read(char[] cbuf)
<source lang="java">
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); }
}
</source>
new FileReader(String name)
<source lang="java">
/*
* 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); } }
}
</source>