前言
這是今天對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(); //調試來測試和分析集合框架