@SuppressWarnings("finally")

@SuppressWarnings("unused") JDK5.0后的新特性,你在使用IDE如eclipse的时候,当你定义了一个变量如int a=0;但是你后面根本就没有使用到这个变量,这一行的前面会有一个黄色的警告标志,你将鼠标移动到上面会提示“这个变量从未被使用”,你用上面的标注后就没有这个提示了。

 

J2SE 提供的最后一个批注是 @SuppressWarnings。该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。 

一点背景:J2SE 5.0 为 Java 语言增加了几个新的特性,并且和它们一起增加了许多新的警告并承诺在将来增加更多的警告。您可以为 "javac" 增加 -Xlint 参数来控制是否报告这些警告(如上面的 @Deprecated 部分所示)。 

默认情况下,Sun 编译器以简单的两行的形式输出警告。通过添加-Xlint:keyword 标记(例如 -Xlint:finally),您可以获得关键字类型错误的完整说明。通过在关键字前面添加一个破折号,写为 -Xlint:-keyword,您可以取消警告。(-Xlint 支持的关键字的完整列表可以在 javac 文档页面上找到。)下面是一个清单: 

关键字 用途 
deprecation 使用了不赞成使用的类或方法时的警告 
unchecked
执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型。
fallthrough 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。 
path 在类路径、源文件路径等中有不存在的路径时的警告。 
serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告。 
fin
ally 任何 finally 子句不能正常完成时的警告。 
all 关于以上所有情况的警告。 

@SuppressWarnings
批注允许您选择性地取消特定代码段(即,类或方法)中的警告。其中的想法是当您看到警告时,您将调查它,如果您确定它不是问题,您就可以添加一个 @SuppressWarnings 批注,以使您不会再看到警告。虽然它听起来似乎会屏蔽潜在的错误,但实际上它将提高代码安全性,因为它将防止您对警告无动于衷您看到的每一个警告都将值得注意。 

下面是使用 @SuppressWarnings 来取消 deprecation 警告的一个例子: 

public class DeprecatedExample2 {
@Deprecated
public static void foo() {
}
}

public class DeprecatedUser2 {
@SuppressWarnings(value={"deprecation"})
public static void main(String[] args) {
DeprecatedExample2.foo();
}
}

@SuppressWarnings 批注接收一个 "value" 变量,该变量是一个字符串数组,它指示将取消的警告。合法字符串的集合随编译器而变化,但在 JDK 上,可以传递给 -Xlint 的是相同的关键字集合(非常方便)。并且要求编译器忽略任何它们不能识别的关键字,这在您使用一些不同的编译器时非常方便。 

因为 @SuppressWarnings 批注仅接收一个参数,并为该参数使用了特殊的名称 "value",所以您可以选择省略 value=,作为一种方便的缩写: 

public class DeprecatedUser2 {
@SuppressWarnings({"deprecation"})
public static void main(String[] args) {
DeprecatedExample2.foo();
}
}

您可以将单个数组参数中的任意数量的字符串值传递给批注,并在任何级别上放置批注。例如,以下示例代码指示将取消整个类的 deprecation 警告,而仅在 main() 方法代码内取消 unchecked 和 fallthrough 警告: 

import java.util.*;

@SuppressWarnings({"deprecation"})
public class NonGenerics {

@SuppressWarnings({"unchecked","fallthrough"})
public static void main(String[] args) {
Runtime.runFinalizersOnExit()();

List list = new ArrayList();
list.add("foo");
}

public static void foo() {
List list = new ArrayList();
list.add("foo");
}
}

@SuppressWarnings 是否比前两个批注更有用?绝对是这样。不过,在 JDK 1.5.0 版本中还没有完全支持该批注,如果您用 1.5.0 来尝试它,那么它将类似无操作指令。调用-Xlint:-deprecation 也没有任何效果。Sun 没有声明什么时候将增加支持,但它暗示这将在即将推出的一个 dot 版本中实现。 

更进一步 

如果您试图在 Javadocs 页面中查看这些属性,那么您可能很难找到它们。它们位于核心的 java.lang包中,但有点隐蔽,它们出现在 Javadoc 类的最底端,列在 Exceptions 和 Errors 后面。 

注意到了附加在 SuppressWarnings 批注后面的陌生的批注 @Target 和 @Retention 了吗?这些称为元数据批注,它们描述了该批注在哪里适用。我将在本系列的第二篇文章中介绍它们,以及介绍如何将元数据批注应用到您自己的批注中。

参考资料:http://www.javaeye.com/topic/231224


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java中的IO操作总结(四) 前面已经把java io的主要操作讲完了 这一节我们来说说关于java io的其他内容 Serializable序列化 实例1:对象的序列化 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; @SuppressWarnings("serial") //一个类要想实现序列化则必须实现Serializable接口 class Person implements Serializable { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Name:" + this.name + ", Age:" + this.age; } } public class Demo { public static void main(String[] args) { String path = File.separator + "home" + File.separator + "siu" + File.separator + "work" + File.separator + "demo.txt"; Person p1 = new Person("zhangsan",12); Person p2 = new Person("lisi",14); //此处创建文件写入流的引用是要给ObjectOutputStream的构造函数玩儿 FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(path); oos = new ObjectOutputStream(fos); //这里可以写入对象,也可以写入其他类型数据 oos.writeObject(p1); oos.writeObject(p2); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } 解压密码 www.jiangyea.com
J2SE 提供的最后一个批注是 @SuppressWarnings。该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。 一点背景:J2SE 5.0 为 Java 语言增加了几个新的特性,并且和它们一起增加了许多新的警告并承诺在将来增加更多的警告。您可以为 "javac" 增加 -Xlint 参数来控制是否报告这些警告(如上面的 @Deprecated 部分所示)。 默认情况下,Sun 编译器以简单的两行的形式输出警告。通过添加 -Xlint:keyword 标记(例如 -Xlint:finally),您可以获得关键字类型错误的完整说明。通过在关键字前面添加一个破折号,写为 -Xlint:-keyword,您可以取消警告。(-Xlint 支持的关键字的完整列表可以在 javac 文档页面上找到。)下面是一个清单: 关键字 用途 deprecation 使用了不赞成使用的类或方法时的警告 unchecked 执行了未检查的转换时的警告,例如当使用集合时没有用泛型 (Generics) 来指定集合保存的类型。 fallthrough 当 Switch 程序块直接通往下一种情况而没有 Break 时的警告。 path 在类路径、源文件路径等中有不存在的路径时的警告。 serial 当在可序列化的类上缺少 serialVersionUID 定义时的警告。 finally 任何 finally 子句不能正常完成时的警告。 all 关于以上所有情况的警告。 @SuppressWarnings 批注允许您选择性地取消特定代码段(即,类或方法)中的警告。其中的想法是当您看到警告时,您将调查它,如果您确定它不是问题,您就可以添加一个 @SuppressWarnings 批注,以使您不会再看到警告。虽然它听起来似乎会屏蔽潜在的错误,但实际上它将提高代码安全性,因为它将防止您对警告无动于衷 — 您看到的每一个警告都将值得注意。 下面是使用 @SuppressWarnings 来取消 deprecation 警告的一个例子: public class DeprecatedExample2 { @Deprecated public static void foo() { }}public class DeprecatedUser2 { @SuppressWarnings(value={"deprecation"})public static void main(String[] args) { DeprecatedExample2.foo(); }} @SuppressWarnings 批注接收一个 "value" 变量,该变量是一个字符串数组,它指示将取消的警告。合法字符串的集合随编译器而变化,但在 JDK 上,可以传递给 -Xlint 的是相同的关键字集合(非常方便)。并且要求编译器忽略任何它们不能识别的关键字,这在您使用一些不同的编译器时非常方便。 因为 @SuppressWarnings 批注仅接收一个参数,并为该参数使用了特殊的名称 "value",所以您可以选择省略 value=,作为一种方便的缩写: public class DeprecatedUser2 { @SuppressWarnings({"deprecation"})public static void main(String[] args) { DeprecatedExample2.foo(); }} 您可以将单个数组参数中的任意数量的字符串值传递给批注,并在任何级别上放置批注。例如,以下示例代码指示将取消整个类的 deprecation 警告,而仅在 main() 方法代码内取消 unchecked 和 fallthrough 警告: import java.util.*;@SuppressWarnings({"deprecation"})public class NonGenerics { @SuppressWarnings({"unchecked","fallthrough"})public static void main(String[] args) { Runtime.runFinalizersOnExit(); List list = new ArrayList(); list.add("foo"); } public static void foo() { List list = new ArrayList(); list.add("foo"); }} @SuppressWarnings 是否比前两个批注更有用?绝对是这样。不过,在 JDK 1.5.0 版本中还没有完全支持该批注,如果您用 1.5.0 来尝试它,那么它将类似无操作指令。调用 -Xlint:-deprecation 也没有任何效果。Sun 没有声明什么时候将增加支持,但它暗示这将在即将推出的一个 dot 版本中实现。 更进一步 如果您试图在 Javadocs 页面中查看这些属性,那么您可能很难找到它们。它们位于核心的 java.lang 包中,但有点隐蔽,它们出现在 Javadoc 类的最底端,列在 Exceptions 和 Errors 后面。 注意到了附加在 SuppressWarnings 批注后面的陌生的批注 @Target 和 @Retention 了吗?这些称为元数据批注,它们描述了该批注在哪里适用。我将在本系列的第二篇文章中介绍它们,以及介绍如何将元数据批注应用到您自己的批注中。
package com.hexiang.utils; import java.io.*; import java.util.*; import java.util.zip.Adler32; import java.util.zip.CheckedInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipUtils { private static final int BUFFER = 8192; private static void log(String msg){ System.out.println (msg); } private static String getFileName(String filePath){ int index = filePath.indexOf("."); return filePath.substring(0, index); } @SuppressWarnings("unused") private static String getRootPath(String filePath){ int index = filePath.indexOf("."); return filePath.substring(0, index); } public static void zip(String sourceFilePath){ File fileDir = new File(sourceFilePath); if(fileDir.exists()){ log(fileDir.getPath()+" Starting Zip ..."); long startTime = System.currentTimeMillis(); doZip(fileDir); long endTime = System.currentTimeMillis(); long costTime = endTime - startTime; log("Zip Success!"); log("use time -- "+costTime+" millsec!"); }else{ log("can't find the File!"); } } public static void unZip(String zipFilePath){ File fileDir = new File(zipFilePath); if(fileDir.exists()){ log(fileDir.getPath()+" Starting UnZip ..."); long startTime = System.currentTimeMillis(); doUnZip(fileDir); long endTime = System.currentTimeMillis(); long costTime = endTime - startTime; log("UnZip Success!"); log("use time -- "+costTime+" millsec!"); }else{ log("can't find the File!"); } } public static void doZip(File file){ List<File> fileList = new ArrayList<File>(); List<File> allFiles = (ArrayList<File>)searchFiles(file.getPath(), fileList); Object[] fileArray = allFiles.toArray(); BufferedInputStream in = null; FileInputStream fis = null; ZipOutputStream zos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(file.getParent()+File.separator+file.getName()+".zip"); zos = new ZipOutputStream(new BufferedOutputStream(fos, BUFFER)); zos.setLevel(9); byte[] data = new byte [BUFFER]; for (int i = 0; i<fileArray.length; i++){ // 设置压缩文件入口entry,为被读取的文件创建压缩条目 File tempFile = new File(fileArray[i].toString()); String rootStr = file.getPath(); String entryStr = null; // entry以相对路径的形式设置。以文件夹C:\temp例如temp\test.doc或者test.xls 如果设置不当,会出现拒绝访问等错误 // 分别处理单个文件/目录的entry if(rootStr.equals(tempFile.getPath())){ entryStr = tempFile.getName(); }else{ entryStr = tempFile.getPath().substring((rootStr+File.separator).length()); } log(entryStr); ZipEntry entry = new ZipEntry(entryStr); zos.putNextEntry(entry); fis = new FileInputStream(tempFile); in = new BufferedInputStream(fis, BUFFER); int count; while((count = in.read(data, 0, BUFFER)) != -1){ zos.write(data, 0, count); } } } catch (Exception ex) { ex.printStackTrace(); }finally{ try{ if(in != null){ in.close(); } if(zos != null){ zos.close(); } }catch (Exception e) { e.printStackTrace(); } } } public static void doUnZip(File file){ try{ final int BUFFER = 2048; BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(file); CheckedInputStream checksum = new CheckedInputStream(fis,new Adler32()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum)); ZipEntry entry; while((entry = zis.getNextEntry()) != null){ log("Extracting: "+entry); int count; byte[] data = new byte[BUFFER]; log("unzip to "+getFileName(file.getPath())); FileOutputStream fos = new FileOutputStream(getFileName(file.getPath())+File.separator+newDir(file, entry.getName())); dest = new BufferedOutputStream(fos, BUFFER); while((count = zis.read(data, 0, BUFFER)) != -1){ dest.write(data, 0, count); } dest.flush(); dest.close(); } zis.close(); System.out.println("Checksum: "+checksum.getChecksum().getValue()); }catch(Exception e){ e.printStackTrace(); } } public static List<File> searchFiles(String sourceFilePath, List<File> fileList){ File fileDir = new File(sourceFilePath); if(fileDir.isDirectory()){ File[] subfiles = fileDir.listFiles(); for(int i = 0; i<subfiles.length; i++){ searchFiles(subfiles[i].getPath(),fileList); } }else{ fileList.add(fileDir); } return fileList; } @SuppressWarnings("static-access") private static String newDir(File file,String entryName) { String rootDir=getFileName(file.getPath()); log("root:"+rootDir); int index = entryName.lastIndexOf("\\"); String dirStr=new File(rootDir).getParent(); log(dirStr); if (index != -1) { String path=entryName.substring(0, index); log("new Dir:"+rootDir+file.separator+path); new File(rootDir+file.separator+path).mkdirs(); log("entry:"+entryName.substring(0, index)); } else{ new File(rootDir).mkdirs(); log("entry:"+entryName); } return entryName; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值