当前位置: 代码迷 >> Java相关 >> 关于泛型!
  详细解决方案

关于泛型!

热度:200   发布时间:2007-01-16 00:01:59.0
关于泛型!
import java.io.*;
import java.util.*;
public class TextFile extends ArrayList
{
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();
}
public static void main(String[] args) throws Exception
{
String f=read("TextFile.java");
write("test.txt",f);
TextFile text=new TextFile("test.txt");
text.write("test2.txt");
}
}
编译不通过..
请帮忙看下,解释一下..
谢谢帮忙.
搜索更多相关的解决方案: 泛型  

----------------解决方案--------------------------------------------------------
[CODE]import java.io.*;
import java.util.*;
public class TextFile<E> extends ArrayList<E>
{
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(Collection<E> c) throws IOException
{
super(c);
}
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();
}
public static void main(String[] args) throws Exception
{
String f=read("TextFile.java");
write("test.txt",f);
TextFile<String> text=new TextFile<String>(Arrays.<String>asList(read("test.txt").split("\n")));

text.write("test2.txt");
}
}[/CODE]

改成这样就不会有警告了

你的代码是可以编译通过的,只是有警告而已
----------------解决方案--------------------------------------------------------
  相关解决方案