javac中的未检查或不安全操作错误
我正在完成学校的一项实验作业,在编译时出现了这个错误。程序运行正常,bit希望修复导致错误的原因。下面是程序代码和完整错误。一如既往地谢谢你 注意:使用-Xlint重新编译:未选中以获取详细信息javac中的未检查或不安全操作错误,java,Java,我正在完成学校的一项实验作业,在编译时出现了这个错误。程序运行正常,bit希望修复导致错误的原因。下面是程序代码和完整错误。一如既往地谢谢你 注意:使用-Xlint重新编译:未选中以获取详细信息 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ie.moguntia.webcrawler;
import java.net.*;
import java.io.*;
import java.util.*;
/**
*
* @author Cong
*/
public class SaveURL
{
/**
* Opens a buffered stream on the url and copies the contents to writer
*/
public static void saveURL(URL url, Writer writer)
throws IOException {
BufferedInputStream in = new BufferedInputStream(url.openStream());
for (int c = in.read(); c != -1; c = in.read()) {
writer.write(c);
}
}
/**
* Opens a buffered stream on the url and copies the contents to OutputStream
*/
public static void saveURL(URL url, OutputStream os)
throws IOException {
InputStream is = url.openStream();
byte[] buf = new byte[1048576];
int n = is.read(buf);
while (n != -1) {
os.write(buf, 0, n);
n = is.read(buf);
}
}
/**
* Writes the contents of the url to a string by calling saveURL with a
* string writer as argument
*/
public static String getURL(URL url)
throws IOException {
StringWriter sw = new StringWriter();
saveURL(url, sw);
return sw.toString();
}
/**
* Writes the contents of the url to a new file by calling saveURL with
* a file writer as argument
*/
public static void writeURLtoFile(URL url, String filename)
throws IOException {
// FileWriter writer = new FileWriter(filename);
// saveURL(url, writer);
// writer.close();
FileOutputStream os = new FileOutputStream(filename);
saveURL(url, os);
os.close();
}
/**
* Extract links directly from a URL by calling extractLinks(getURL())
*/
public static Vector extractLinks(URL url)
throws IOException {
return extractLinks(getURL(url));
}
public static Map extractLinksWithText(URL url)
throws IOException {
return extractLinksWithText(getURL(url));
}
/**
* Extract links from a html page given as a raw and a lower case string
* In order to avoid the possible double conversion from mixed to lower case
* a second method is provided, where the conversion is done externally.
*/
public static Vector extractLinks(String rawPage, String page) {
int index = 0;
Vector links = new Vector();
while ((index = page.indexOf("<a ", index)) != -1)
{
if ((index = page.indexOf("href", index)) == -1) break;
if ((index = page.indexOf("=", index)) == -1) break;
String remaining = rawPage.substring(++index);
StringTokenizer st
= new StringTokenizer(remaining, "\t\n\r\"'>#");
String strLink = st.nextToken();
if (! links.contains(strLink)) links.add(strLink);
}
return links;
}
/**
* Extract links (key) with link text (value)
* Note that due to the nature of a Map only one link text is returned per
* URL, even if a link occurs multiple times with different texts.
*/
public static Map extractLinksWithText(String rawPage, String page) {
int index = 0;
Map links = new HashMap();
while ((index = page.indexOf("<a ", index)) != -1)
{
int tagEnd = page.indexOf(">", index);
if ((index = page.indexOf("href", index)) == -1) break;
if ((index = page.indexOf("=", index)) == -1) break;
int endTag = page.indexOf("</a", index);
String remaining = rawPage.substring(++index);
StringTokenizer st
= new StringTokenizer(remaining, "\t\n\r\"'>#");
String strLink = st.nextToken();
String strText = "";
if (tagEnd != -1 && tagEnd + 1 <= endTag) {
strText = rawPage.substring(tagEnd + 1, endTag);
}
strText = strText.replaceAll("\\s+", " ");
links.put(strLink, strText);
}
return links;
}
/**
* Extract links from a html page given as a String
* The return value is a vector of strings. This method does neither check
* the validity of its results nor does it care about html comments, so
* links that are commented out are also retrieved.
*/
public static Vector extractLinks(String rawPage) {
return extractLinks(rawPage, rawPage.toLowerCase().replaceAll("\\s", " "));
}
public static Map extractLinksWithText(String rawPage) {
return extractLinksWithText(rawPage, rawPage.toLowerCase().replaceAll("\\s", " "));
}
/**
* As a standalone program this class is capable of copying a url to a file
*/
public static void main(String[] args) {
try {
if (args.length == 1) {
URL url = new URL(args[0]);
System.out.println("Content-Type: " +
url.openConnection().getContentType());
// Vector links = extractLinks(url);
// for (int n = 0; n < links.size(); n++) {
// System.out.println((String) links.elementAt(n));
// }
Set links = extractLinksWithText(url).entrySet();
Iterator it = links.iterator();
while (it.hasNext()) {
Map.Entry en = (Map.Entry) it.next();
String strLink = (String) en.getKey();
String strText = (String) en.getValue();
System.out.println(strLink + " \"" + strText + "\" ");
}
return;
} else if (args.length == 2) {
writeURLtoFile(new URL(args[0]), args[1]);
return;
}
} catch (Exception e) {
System.err.println("An error occured: ");
e.printStackTrace();
// System.err.println(e.toString());
}
// Display usage information
// (If the program had done anything sensible, we wouldn't be here.)
System.err.println("Usage: java SaveURL <url> [<file>]");
System.err.println("Saves a URL to a file.");
System.err.println("If no file is given, extracts hyperlinks on url to console.");
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包ie.moguntia.webcrawler;
导入java.net。*;
导入java.io.*;
导入java.util.*;
/**
*
*@作者丛
*/
公共类SaveURL
{
/**
*在url上打开缓冲流并将内容复制到writer
*/
公共静态void saveURL(URL URL、编写器)
抛出IOException{
BufferedInputStream in=新的BufferedInputStream(url.openStream());
for(int c=in.read();c!=-1;c=in.read()){
write.write(c);
}
}
/**
*在url上打开缓冲流,并将内容复制到OutputStream
*/
公共静态void saveURL(URL URL、OutputStream os)
抛出IOException{
InputStream=url.openStream();
字节[]buf=新字节[1048576];
int n=正在读取(buf);
而(n!=-1){
os.write(buf,0,n);
n=是读数(buf);
}
}
/**
*通过使用
*字符串编写器作为参数
*/
公共静态字符串getURL(URL URL)
抛出IOException{
StringWriter sw=新的StringWriter();
saveURL(url,sw);
返回sw.toString();
}
/**
*通过使用调用saveURL将url的内容写入新文件
*作为参数的文件编写器
*/
公共静态void writeURLtoFile(URL、字符串文件名)
抛出IOException{
//FileWriter writer=新的FileWriter(文件名);
//saveURL(url,writer);
//writer.close();
FileOutputStream os=新的FileOutputStream(文件名);
saveURL(url,os);
os.close();
}
/**
*通过调用extractLinks(getURL())直接从URL提取链接
*/
公共静态向量提取链接(URL)
抛出IOException{
返回提取链接(getURL(url));
}
公共静态映射extractLinksWithText(URL)
抛出IOException{
返回extractLinksWithText(getURL(url));
}
/**
*从作为原始字符串和小写字符串给定的html页面提取链接
*为了避免可能的从混合到小写的双重转换
*提供了第二种方法,其中转换在外部完成。
*/
公共静态向量提取链接(字符串页面,字符串页面){
int指数=0;
向量链接=新向量();
而((index=page.indexOf(您使用的是具有泛型类型参数的几个类的原始(即非泛型)形式,包括
- 地图
- 哈希映射
- 载体
- 迭代器
- 设置
- 地图入口
通过提供适当的类型参数,使用这些类的泛型形式