前言
这是今天对Java复习知识点和学习到内容的一些记录 , 其实这些知识点很简单 , 而且非常实用.
知识点总结
- 解压文件和目录
- ZipInputStream
- ZipEntry
- ZipFile
- 给图片添加文字水印
- ImageIO
- BufferedImage
添加图片水印的方法和这个差不多.
- 集合框架
- List
- set
- map
测试截图
代码
解压文件夹和解压文件
/**
* 1. 解压文件和目录 - 上午学会的知识点回顾
* 2. 给图片添加文字水印
* @author Administrator
*
*/
public class FileTestMy {
/**
* 解压文件
* @param zipFilePath
* @param saveFilePath
* @param zipFileName
* @return
*/
public static boolean unZipFile(String zipFilePath , String saveFilePath , String zipFileName ) {
try {
/**输入流对象**/
ZipFile zipFile = new ZipFile(new File(zipFilePath ) );
ZipEntry entry = new ZipEntry(zipFileName );
BufferedInputStream input = new BufferedInputStream(zipFile.getInputStream(entry ) );
/**输出流对象**/
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(saveFilePath ) );
/**开始将解压文件**/
byte[] buf = new byte[input.available() ];
int ch = input.read(buf );
while(ch != 0 && ch != -1 ) {
output.write(buf , 0 , ch );
ch = input.read(buf );
}
/**释放文件**/
output.flush();
output.close();
input.close();
return true;
}catch(Exception e ) {
e.printStackTrace();
}
return false;
}
/**
* 解压文件夹
* @param zipFilePath
* @param saveDirectory
* @return
*/
public static boolean unZipFile(String zipFilePath , String saveDirectory ) {
/**获取输入流对象**/
try {
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(new File(zipFilePath ) ) );
ZipFile zipFile = new ZipFile(zipFilePath );
ZipEntry entry = null;
BufferedInputStream zipInputStream = null; //每个内部压缩文件的输入流
/**获取输出流对象**/
BufferedOutputStream fileOutput = null; //压缩到外部的输出流
File outputFile = null;
/**开始解压所有文件**/
while((entry = zipInput.getNextEntry()) != null ) {
if(saveDirectory != null ) {
//解压到用户指定目录
outputFile = new File(saveDirectory + File.separator + entry.getName() );
}else {
//解压到当前文件夹的目录下
outputFile = new File(new File(zipFilePath ).getParentFile() + File.separator + entry.getName() );
}
//创建目录或创建文件
if(! outputFile.getParentFile().exists() ) {
//表示有目录
outputFile.getParentFile().mkdir();
}
if(! outputFile.exists() ) {
outputFile.createNewFile();
}
/**
* 需要注意 , 解压文件时获取entry对象 , 如果有目录的话会获取到目录 , 所以碰到目录要跳过.
*/
if(entry.isDirectory() ) {
break;
}
//创建压缩到输出流
zipInputStream = new BufferedInputStream(zipFile.getInputStream(entry ) );
fileOutput = new BufferedOutputStream(new FileOutputStream(outputFile ) );
byte[] buf = new byte[zipInputStream.available() ];
int ch = zipInputStream.read(buf );
while(ch != 0 && ch != -1 ) {
fileOutput.write(buf , 0 , ch );
ch = zipInputStream.read(buf );
}
//释放资源
zipInputStream.close();
fileOutput.flush();
fileOutput.close();
}
return true;
}catch(Exception e ) {
e.printStackTrace();
}
return false;
}
图片添加文字水印
/**
* 图片添加文字水印 - 复习
* 图片水印就不添加了.
* @param addImageFile
* @param line
* @return
*/
public static boolean addStringPicture(String addImageFile , String line ) {
try {
BufferedImage buffImage = ImageIO.read(new File(addImageFile ) );
Graphics g = buffImage.getGraphics();
//获取文件大小 , 确定水印文字写入位置
int width = buffImage.getWidth();
int height = buffImage.getHeight();
int x = width / 2 - 10;
int y = height / 2 ;
//设置颜色
g.setColor(Color.white );
g.setFont(new Font("Microsoft YaHei" , Font.BOLD , 30 ) );
//绘制文字水印
g.drawString(line , x , y );
//保存文件
return ImageIO.write(buffImage , "png" , new FileOutputStream(addImageFile ));
}catch(Exception e ) {
e.printStackTrace();
}
return false;
}
集合框架
public static void Collections() {
//List 里面的值是有序 , 可以重复
List<Integer> list = new ArrayList(); //连续存储
List<Integer > lists1 = new LinkedList<>(); //链式存储
//Set 里面的值是无序 , 里面的值不可以重复
Set<Integer > hashSet = new HashSet<>();
Set<Integer > treeSet = new TreeSet<>();
//Map 键队值 一个key一个val
Map<Integer , String > maps = new HashMap<>();
//使用方法基本都差不多.
list.add(1 );
list.add(1 );
list.add(3 );
lists1.add(1 );
lists1.add(1 );
lists1.add(3 );
hashSet.add(1 );
hashSet.add(1 );
hashSet.add(3 );
treeSet.add(1 );
treeSet.add(1 );
treeSet.add(3 );
maps.put(1 , "test" );
maps.put(2 , "ghoset" );
//其实的看笔记里留下的.
}
测试方法
public static void main(String[] args) {
String zipFilePath = "F:\\JAVA测试\\111\\ttt1.zip";
String saveFilePath = "F:\\JAVA测试\\111\\test.txt";
String zipFileName = "test.txt";
/* boolean flag = unZipFile(zipFilePath, saveFilePath, zipFileName );
if(flag ) {
System.out.println("解压文件成功!" );
}else {
System.out.println("解压文件失败!" );
}*/
/* String saveDirectory = "F:\\JAVA测试\\111";
boolean flag = unZipFile(zipFilePath , saveDirectory );
if(flag ) {
System.out.println("解压文件成功!" );
}else {
System.out.println("解压文件失败!" );
}
*/
/**添加文字水印 , 对以前知识点的复习**/
/* String picturePath = "F:\\psb.jpg";
String line = "Ghoset is Very Good!" ;
boolean flag = addStringPicture(picturePath , line );
if(flag ) {
System.out.println("添加文字水印成功!" );
}else {
System.out.println("添加文字水印失败!" );
}
*/
/**集合框架的使用**/
Collections(); //调试来测试和分析集合框架