How to Convert File to a Different Encoding
From Java Example Source Code
Contents |
[edit] Overview - How to Convert File to a Different Encoding
This Java example program shows how to convert file to a different encoding with org.apache.commons.io.FileUtils.
[edit] Java Source Code
- Package: com.exampleshow.file
- File: ConvertFileEncode.java
package com.exampleshow.file; import java.io.File; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; /** * @author Code Panda * Copyright (C) 2008 Java ExampleShow (http://java.exampleshow.com) * * This code is free software; you can redistribute it and/or modify it. * However, this header must remain intact and unchanged. Additional * information may be appended after this header. Publications based on * this code must also include an appropriate reference. * * This code is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. * */ public class ConvertFileEncode { /** * */ public ConvertFileEncode() { // TODO Auto-generated constructor stub } public void convertAllFile(String srcPath, String srcEncode, String targetPath, String targetEncode, String[] fileFilters) throws Exception { List<File> fileList = new ArrayList( FileUtils.listFiles(new File(srcPath), fileFilters, true) ); for(int i=0; i<fileList.size(); i++) { File file = fileList.get(i); String srcFilePath = file.getAbsolutePath(); File targetFile = new File(targetPath, srcFilePath.substring(srcPath.length() ) ); convert(file, srcEncode, targetFile, targetEncode); } } public void convert(File srcFile, String srcEncode, File targetFile, String targetEncode) throws Exception { System.out.println("try "+srcFile.getAbsolutePath() + " -> " + targetFile.getAbsolutePath()); String content = FileUtils.readFileToString(srcFile, srcEncode); File targetParentFile = targetFile.getParentFile(); if(!targetParentFile.exists()) targetFile.getParentFile().mkdirs(); FileUtils.writeStringToFile(targetFile, content, targetEncode); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ConvertFileEncode converter = new ConvertFileEncode(); try { converter.convertAllFile("c:/exampleshow", "GBK", "c:/exampleshow.utf8", "UTF-8", new String[]{"txt","html","php"}); } catch (Exception e) { e.printStackTrace(); } } }
[edit] What Result You Can Get
Run the program, you will get the list of files converted by this program. For example:
try c:\exampleshow\exampleshow.txt -> c:\exampleshow.utf8\exampleshow.txt
[edit] Required External Libraries and/or Files for this Java Example
In order to run this program, the following libraries are required:
- Apache Commons IO. Collection of I/O utilities. IO 1.4 API Download Site
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
