处理json文件中的文件名
package com.vfsd.core; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ReadJsonAndSaveNewFile { private static List<String> json_list = new ArrayList<String>(); public static void main(String[] args) { String folder="G:\json_data"; String endStr=".json"; boolean isTrue=true; listFilesByEndStr(folder, endStr,isTrue); listJsonFile(); } public static void listFilesByEndStr(String folder,String endStr,boolean isTrue) { File folder1 = new File(folder); File[] files = folder1.listFiles(); for(File indexFile:files) { if(indexFile.isDirectory()) { String folderPath = indexFile.getAbsolutePath(); listFilesByEndStr(folderPath,endStr,isTrue); }else if(indexFile.isFile()) { String fileName = indexFile.getName(); if(isTrue) { if(fileName.endsWith(endStr)) { String folderPath = indexFile.getAbsolutePath(); //System.out.println(folderPath); json_list.add(folderPath); } }else { if(!fileName.endsWith(endStr)) { //System.out.println(fileName); } } } } } public static void listJsonFile() { String outputPath = "G:\json_data2"; int num=1000; for(int k=0;k<json_list.size();k++){ String indexJsonFilePath = json_list.get(k); System.out.println(indexJsonFilePath); String imgFilePath = indexJsonFilePath.replace(".json", ".jpg"); System.out.println(imgFilePath); String newJsonFilePath = outputPath+"\"+num+".json"; String newImgFilePath = outputPath+"\"+num+".jpg"; String imgName = num+".jpg"; try { copyImg(imgFilePath,newImgFilePath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { copyJson(indexJsonFilePath,newJsonFilePath,imgName); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } num++; } } public static void copyImg(String oldImgPath,String newImgPath) throws IOException{ File oldFile = new File(oldImgPath); File newFile = new File(newImgPath); FileInputStream fileInputStreamObj = new FileInputStream(oldFile); FileOutputStream filOutputStreamObj = new FileOutputStream(newFile); byte[] bytes = new byte[1024]; int len=0; while((len=fileInputStreamObj.read(bytes))!=-1){ filOutputStreamObj.write(bytes, 0, len); } filOutputStreamObj.flush(); filOutputStreamObj.close(); fileInputStreamObj.close(); } public static void copyJson(String oldJsonPath,String newJsonPath,String imgName) throws IOException{ File oldJsonFile = new File(oldJsonPath); File newJsonFile = new File(newJsonPath); FileReader fileReaderObj = new FileReader(oldJsonFile); BufferedReader bufferedReaderObj = new BufferedReader(fileReaderObj); FileWriter fileWriterObj = new FileWriter(newJsonFile); BufferedWriter bufferedWriterObj = new BufferedWriter(fileWriterObj); String lineStr=""; while((lineStr=bufferedReaderObj.readLine())!=null) { if(lineStr.contains("imagePath")){ // "imagePath": "u=46225769,59147520&fm=26&fmt=auto&gp=0-1414246.jpg", lineStr=""imagePath": ""+imgName+"","; } bufferedWriterObj.write(lineStr+" "); } bufferedWriterObj.flush(); fileWriterObj.flush(); bufferedWriterObj.close(); fileWriterObj.close(); bufferedReaderObj.close(); fileReaderObj.close(); } /// }
G:json_datau=973425934,2427009704&fm=26&fmt=auto&gp=0-8539165.json G:json_datau=973425934,2427009704&fm=26&fmt=auto&gp=0-8539165.jpg G:json_datau=98157198,2014981619&fm=26&fmt=auto&gp=0-5779385.json G:json_datau=98157198,2014981619&fm=26&fmt=auto&gp=0-5779385.jpg G:json_datau=994802799,2240003585&fm=26&fmt=auto&gp=0-8749040.json G:json_datau=994802799,2240003585&fm=26&fmt=auto&gp=0-8749040.jpg G:json_datau=999068942,1187608502&fm=26&fmt=auto&gp=0-7583789.json G:json_datau=999068942,1187608502&fm=26&fmt=auto&gp=0-7583789.jpg
1000.jpg
1000.json
1001.jpg
1001.json
1002.jpg
1002.json
同时也修改了json文件中的imagePath对应的值
"imagePath": "1000.jpg",
##################3