📄 tij0185.html
字号:
Pr.error("closing " + os);
}
}
<font color="#0000ff">static</font> <font color="#0000ff">void</font> close(Reader os) {
<font color="#0000ff">try</font> {
os.close();
} <font color="#0000ff">catch</font>(IOException e) {
Pr.error("closing " + os);
}
}
}
<font color="#0000ff">class</font> SourceCodeFile {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">final</font> String
startMarker = "<font color="#009900">//:", // Start of source file</font>
endMarker = "} <font color="#009900">///:~", // End of source</font>
endMarker2 = "}; <font color="#009900">///:~", // C++ file end</font>
beginContinue = "} <font color="#009900">///:Continued",</font>
endContinue = "<font color="#009900">///:Continuing",</font>
packMarker = "###", <font color="#009900">// Packed file header tag</font>
eol = <font color="#009900">// Line separator on current system</font>
System.getProperty("line.separator"),
filesep = <font color="#009900">// System's file path separator</font>
System.getProperty("file.separator");
<font color="#0000ff">public</font> <font color="#0000ff">static</font> String copyright = "";
<font color="#0000ff">static</font> {
<font color="#0000ff">try</font> {
BufferedReader cr =
<font color="#0000ff">new</font> BufferedReader(
<font color="#0000ff">new</font> FileReader("Copyright.txt"));
String crin;
<font color="#0000ff">while</font>((crin = cr.readLine()) != <font color="#0000ff">null</font>)
copyright += crin + "\n";
cr.close();
} <font color="#0000ff">catch</font>(Exception e) {
copyright = "";
}
}
<font color="#0000ff">private</font> String filename, dirname,
contents = <font color="#0000ff">new</font> String();
<font color="#0000ff">private</font> <font color="#0000ff">static</font> String chapter = "c02";
<font color="#009900">// The file name separator from the old system:</font>
<font color="#0000ff">public</font> <font color="#0000ff">static</font> String oldsep;
<font color="#0000ff">public</font> String toString() {
<font color="#0000ff">return</font> dirname + filesep + filename;
}
<font color="#009900">// Constructor for parsing from document file:</font>
<font color="#0000ff">public</font> SourceCodeFile(String firstLine,
BufferedReader in) {
dirname = chapter;
<font color="#009900">// Skip past marker:</font>
filename = firstLine.substring(
startMarker.length()).trim();
<font color="#009900">// Find space that terminates file name:</font>
<font color="#0000ff">if</font>(filename.indexOf(' ') != -1)
filename = filename.substring(
0, filename.indexOf(' '));
System.out.println("found: " + filename);
contents = firstLine + eol;
<font color="#0000ff">if</font>(copyright.length() != 0)
contents += copyright + eol;
String s;
<font color="#0000ff">boolean</font> foundEndMarker = <font color="#0000ff">false</font>;
<font color="#0000ff">try</font> {
<font color="#0000ff">while</font>((s = in.readLine()) != <font color="#0000ff">null</font>) {
<font color="#0000ff">if</font>(s.startsWith(startMarker))
Pr.error("No end of file marker <font color="#0000ff">for</font> " +
filename);
<font color="#009900">// For this program, no spaces before </font>
<font color="#009900">// the "package" keyword are allowed</font>
<font color="#009900">// in the input source code:</font>
<font color="#0000ff">else</font> <font color="#0000ff">if</font>(s.startsWith("<font color="#0000ff">package</font>")) {
<font color="#009900">// Extract package name:</font>
String pdir = s.substring(
s.indexOf(' ')).trim();
pdir = pdir.substring(
0, pdir.indexOf(';')).trim();
<font color="#009900">// Capture the chapter from the package</font>
<font color="#009900">// ignoring the 'com' subdirectories:</font>
<font color="#0000ff">if</font>(!pdir.startsWith("com")) {
<font color="#0000ff">int</font> firstDot = pdir.indexOf('.');
<font color="#0000ff">if</font>(firstDot != -1)
chapter =
pdir.substring(0,firstDot);
<font color="#0000ff">else</font>
chapter = pdir;
}
<font color="#009900">// Convert package name to path name:</font>
pdir = pdir.replace(
'.', filesep.charAt(0));
System.out.println("<font color="#0000ff">package</font> " + pdir);
dirname = pdir;
}
contents += s + eol;
<font color="#009900">// Move past continuations:</font>
<font color="#0000ff">if</font>(s.startsWith(beginContinue))
<font color="#0000ff">while</font>((s = in.readLine()) != <font color="#0000ff">null</font>)
<font color="#0000ff">if</font>(s.startsWith(endContinue)) {
contents += s + eol;
<font color="#0000ff">break</font>;
}
<font color="#009900">// Watch for end of code listing:</font>
<font color="#0000ff">if</font>(s.startsWith(endMarker) ||
s.startsWith(endMarker2)) {
foundEndMarker = <font color="#0000ff">true</font>;
<font color="#0000ff">break</font>;
}
}
<font color="#0000ff">if</font>(!foundEndMarker)
Pr.error(
"End marker not found before EOF");
System.out.println("Chapter: " + chapter);
} <font color="#0000ff">catch</font>(IOException e) {
Pr.error("Error reading line");
}
}
<font color="#009900">// For recovering from a packed file:</font>
<font color="#0000ff">public</font> SourceCodeFile(BufferedReader pFile) {
<font color="#0000ff">try</font> {
String s = pFile.readLine();
<font color="#0000ff">if</font>(s == <font color="#0000ff">null</font>) <font color="#0000ff">return</font>;
<font color="#0000ff">if</font>(!s.startsWith(packMarker))
Pr.error("Can't find " + packMarker
+ " in " + s);
s = s.substring(
packMarker.length()).trim();
dirname = s.substring(0, s.indexOf('#'));
filename = s.substring(s.indexOf('#') + 1);
dirname = dirname.replace(
oldsep.charAt(0), filesep.charAt(0));
filename = filename.replace(
oldsep.charAt(0), filesep.charAt(0));
System.out.println("listing: " + dirname
+ filesep + filename);
<font color="#0000ff">while</font>((s = pFile.readLine()) != <font color="#0000ff">null</font>) {
<font color="#009900">// Watch for end of code listing:</font>
<font color="#0000ff">if</font>(s.startsWith(endMarker) ||
s.startsWith(endMarker2)) {
contents += s;
<font color="#0000ff">break</font>;
}
contents += s + eol;
}
} <font color="#0000ff">catch</font>(IOException e) {
System.err.println("Error reading line");
}
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> hasFile() {
<font color="#0000ff">return</font> filename != <font color="#0000ff">null</font>;
}
<font color="#0000ff">public</font> String directory() { <font color="#0000ff">return</font> dirname; }
<font color="#0000ff">public</font> String filename() { <font color="#0000ff">return</font> filename; }
<font color="#0000ff">public</font> String contents() { <font color="#0000ff">return</font> contents; }
<font color="#009900">// To write to a packed file:</font>
<font color="#0000ff">public</font> <font color="#0000ff">void</font> writePacked(DataOutputStream out) {
<font color="#0000ff">try</font> {
out.writeBytes(
packMarker + dirname + "#"
+ filename + eol);
out.writeBytes(contents);
} <font color="#0000ff">catch</font>(IOException e) {
Pr.error("writing " + dirname +
filesep + filename);
}
}
<font color="#009900">// To generate the actual file:</font>
<font color="#0000ff">public</font> <font color="#0000ff">void</font> writeFile(String rootpath) {
File path = <font color="#0000ff">new</font> File(rootpath, dirname);
path.mkdirs();
PrintWriter p =
IO.psOpen(<font color="#0000ff">new</font> File(path, filename));
p.print(contents);
IO.close(p);
}
}
<font color="#0000ff">class</font> DirMap {
<font color="#0000ff">private</font> Hashtable t = <font color="#0000ff">new</font> Hashtable();
<font color="#0000ff">private</font> String rootpath;
DirMap() {
rootpath = System.getProperty("user.dir");
}
DirMap(String alternateDir) {
rootpath = alternateDir;
}
<font color="#0000ff">public</font> <font color="#0000ff">void</font> add(SourceCodeFile f){
String path = f.directory();
<font color="#0000ff">if</font>(!t.containsKey(path))
t.put(path, <font color="#0000ff">new</font> Vector());
((Vector)t.get(path)).addElement(f);
}
<font color="#0000ff">public</font> <font color="#0000ff">void</font> writePackedFile(String fname) {
DataOutputStream packed = IO.dosOpen(fname);
<font color="#0000ff">try</font> {
packed.writeBytes("###Old Separator:" +
SourceCodeFile.filesep + "###\n");
} <font color="#0000ff">catch</font>(IOException e) {
Pr.error("Writing separator to " + fname);
}
Enumeration e = t.keys();
<font color="#0000ff">while</font>(e.hasMoreElements()) {
String dir = (String)e.nextElement();
System.out.println(
"Writing directory " + dir);
Vector v = (Vector)t.get(dir);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -