Node.java /** * */
package util;
/**
* @author guhonglueying *
*/
public class Node {
public Node(int x, int y) { this.x = x; this.y = y; }
public int getX() { }
return x; private int x; private int y; public Node() { }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { }
public boolean equals(Object o) { if (!(o instanceof Node))
this.y = y;
}
}
return false;
Node n = (Node) o;
return n.x == x && n.y == y;
public String toString() { }
return x + \
TestMaze.java /** *
*/
package test;
import map.*;
import objectmovable.*; /**
* @author guhonglueying * */
public class TestMaze {
/**
* @param args */
// 主函数,程序入口
public static void main(String[] args) { }
//多态实现:三种方式创建迷宫 SubMap sm = new SimpleMap(); sm.create();
char[][] c = sm.getMap(); //多态实现不同物体的走迷宫过程 SubObjectMovable iom = new Ship(); iom.Maze(c);
}
homework07 文件处理 2011-04-08 22:19
1. 读文件, 添加或去掉行号后写回 2. 统计一个文件中的词个数(请注意中文)
AddRowNum.java package sample;
import java.io.*;
public class AddRowNum { // // // // // // // // // //
System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\System.out.println(\return;
try { /** 开启两个文件, 分别读写 */ BufferedReader reader = new BufferedReader( new FileReader(\
BufferedWriter writer = new BufferedWriter( new FileWriter(\/** 首先取得总行数, 然后逐行添加写回 */ int nRowNum = 0;
String strOneLine = null;
while (reader.readLine() != null) { }
nRowNum++;
public static void main(String[] args) {
reader.close();
String formator = String.format(\nRowNum = 0;
reader = new BufferedReader( new FileReader(\while ((strOneLine = reader.readLine()) != null) {
writer.write(String.format(\ %s\\n\++nRowNum,
strOneLine));
} }
CountWords.java package sample;
import java.io.*;
public class CountWords {
public static void main(String[] args) {
String fileName = \try {
BufferedReader reader = new BufferedReader(new FileReader(fileName)); int NumberCount = 0;
int LetterCount = 0;
int ChineseCharacterCount = 0; int a = -1;
}
(new File(\
writer.close(); reader.close();
new File(\
new File(\
} catch (FileNotFoundException e) { System.out.println(\找不到指定文件\} catch (IOException e) { System.out.println(\文件读写错误\}
Character c;
while ((a = reader.read()) != -1) { c = (char) a; if (Character.isDigit(c)) { NumberCount++;
} else if ('\一' <= c && c <= '\龥') {
ChineseCharacterCount++; } else if (Character.isLetter(c)) { LetterCount++; }
}
reader.close();
System.out.println(\文件\共包含:System.out.println(\数字: \字母: \ + LetterCount + \汉字: \/** 林启敏 */ reader.close();
reader = new BufferedReader(new FileReader(\boolean isWordStart = false;
int count = 0;
char oneChar[] = new char[1];
while (reader.read(oneChar) != -1) { if (oneChar[0] == '.' }
}
|| oneChar[0] == ',' || oneChar[0] == ' ') { if(isWordStart) { }
isWordStart = false; count++;
\
else { }
isWordStart = true;
System.out.println(\
} catch (FileNotFoundException e) { System.out.print(\找不到指定文件\} catch (IOException e) {