public static String getWord(FileReader in) throws IOException {
    // returns the next word found
    final int MaxLen = 255;
    int c, n = 0;
    char[] word = new char[MaxLen];
    // read over non-letters
    while (!Character.isLetter((char) (c = in.read())) && (c != -1)) ;
    // empty while body
    if (c == -1) return ""; // no letter found

    word[n++] = (char) c;
    while (Character.isLetter(c = in.read())) if (n < MaxLen) word[n++] = (char) c;
    return new String(word, 0, n);
  } // end getWord
Beispiel #2
0
 public static void main(String[] args) {
   try {
     if (args.length != 1) throw new IllegalArgumentException("Wrong number of arguments");
     FileReader in = new FileReader(args[0]);
     HardcopyWriter out = null;
     Frame f = new Frame("PrintFile: " + args[0]);
     f.setSize(200, 50);
     f.show();
     try {
       out = new HardcopyWriter(f, args[0], 10, .75, .75, .75, .75);
     } catch (HardcopyWriter.PrintCanceledException e) {
       System.exit(0);
     }
     f.setVisible(false);
     char[] buffer = new char[4096];
     int numchars;
     while ((numchars = in.read(buffer)) != -1) out.write(buffer, 0, numchars);
     out.close();
   } catch (Exception e) {
     System.err.println(e);
     System.err.println("Usage: java HardcopyWriter$PrintFile <filename>");
     System.exit(1);
   }
   System.exit(0);
 }
 private static String fileToString(File f) throws Exception {
   StringWriter output = new StringWriter();
   FileReader input = new FileReader(f);
   char[] buffer = new char[8 * 1024];
   int n = 0;
   while (-1 != (n = input.read(buffer))) {
     output.write(buffer, 0, n);
   }
   return output.toString();
 }
Beispiel #4
0
 public static String readFile(File file) {
   FileReader r = null;
   try {
     r = new FileReader(file);
     char[] data = new char[(int) file.length()];
     r.read(data);
     return new String(data);
   } catch(IOException e) {
     throw Log.errRTExcept(e);
   } finally {
     close(r);
   }
 }
Beispiel #5
0
 public static Character loadCharacter() {
   String filnamn = "saves/charsave.txt";
   String resultat = "";
   try {
     FileReader fr = new FileReader(new File(filnamn));
     int a = fr.read();
     char b;
     while (a != -1) {
       b = (char) a;
       resultat += b;
       a = fr.read();
     }
     fr.close();
   } catch (FileNotFoundException e) {
     System.out.println("Hittade inte filen (" + e.toString() + ")");
   } catch (IOException e) {
     System.out.println("Hoppsan! (" + e.toString() + ")");
   }
   StringTokenizer st = new StringTokenizer(resultat);
   String name = st.nextToken();
   int hp = Integer.parseInt(st.nextToken());
   int skill = Integer.parseInt(st.nextToken());
   return new Character(name, skill, new Weapon(), new Shield());
 }