日期:2014-05-20  浏览次数:20638 次

关于编程思想源码!
命令:javac -Xlint:unckecked TextFile.java
编译结果:TextFile.java:34:警告: [unchecked]对作为普通类型java.util.ArrayList的成员的ArrayList(java.util.Collection<? extends E>)的调用未经检查
super(Arrays.asList(read(fileName).split("\n")));

以下是源码:
//: com:bruceeckel:util:TextFile.java
// Static functions for reading and writing text files as
// a single string, and treating a file as an ArrayList.
// {Clean: test.txt test2.txt}
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.util;
import java.io.*;
import java.util.*;

public class TextFile extends ArrayList {
  // Tools to read and write files as single strings:
  public static String
  read(String fileName) throws IOException {
  StringBuffer sb = new StringBuffer();
  BufferedReader in =
  new BufferedReader(new FileReader(fileName));
  String s;
  while((s = in.readLine()) != null) {
  sb.append(s);
  sb.append("\n");
  }
  in.close();
  return sb.toString();
  }
  public static void
  write(String fileName, String text) throws IOException {
  PrintWriter out = new PrintWriter(
  new BufferedWriter(new FileWriter(fileName)));
  out.print(text);
  out.close();
  }
  public TextFile(String fileName) throws IOException {
  super(Arrays.asList(read(fileName).split("\n")));
  }
  public void write(String fileName) throws IOException {
  PrintWriter out = new PrintWriter(
  new BufferedWriter(new FileWriter(fileName)));
  for(int i = 0; i < size(); i++)
  out.println(get(i));
  out.close();
  }
  // Simple test:
  public static void main(String[] args) throws Exception {
  String file = read("TextFile.java");
  write("test.txt", file);
  TextFile text = new TextFile("test.txt");
  text.write("test2.txt");
  }
} ///:~

------解决方案--------------------
你想问什么?看过Java 1.5里面关于Generics的东西吗?
------解决方案--------------------
我的《THINK IN JAVA》里的光盘是C++的
------解决方案--------------------
用到泛型

JDK 5新增

你的版本应该是JDK5或者以上