Java讀取json字符串 拷貝文件

public static void main(String[] args) {
	String json = "[
		"05605344.jpg",
	"472873996.jpg",
	"488081532.jpg",
	"503827755.jpg",
	"505400883.jpg",
	"545850476.jpg",
	"548509081.jpg",
	"548509082.jpg",
	"642542204.jpg",
	"T110148610.jpg"
		]";
		JSONArray array = JSONArray.parseArray(json );
	String afterPath = "D:\\after";
	for (Object fileName : array) {
		String path = "D:\\before";
		String oldPath = path+"\\"+fileName;
		String newPath = afterPath+"\\"+fileName;
		try {
			File oldFile = new File(oldPath);
			int byteread = 0;
			int bytesum = 0;
			if (oldFile.exists()) {
				InputStream inStream = new FileInputStream(oldPath);
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("複製單個文件操作出錯");
			e.printStackTrace();
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章