Хотел бы применить одну программу для преобразования текстового файла, написанного в кодировке utf-8, в вид, понятный для прочтения мидлетом. Код программы взят http://lib.juga.ru/article/articleview/99/1/16/?PrintableVersion=enabled. В ней автор статьи объясняет, что для этого необходимо: 1) Создать 2 текстовых файла - один исходный, второй - для записи в него преобразованных данных. С этим понятно.
2) Далее автор статьи говорит: "Далее нужно просто запустить конвертор, указав в качестве параметров путь к исходному файлу и путь к результирующему файлу". Здесь мне непонятно - как указать эти параметры.
Как я понял из кода программы - эти данные необходимы для параметров конструкторов класса File. Из документации к классу File я понял, что путь к файлу необходимо указывать в Windows в таком виде: "C:\pathname\toFile\namefile.extension". Если не прав, пожалуйста исправьте.
Не ясно, где именно в коде нужно прописать эти параметры. Здесь полный код программы:
| Код | package ru.quadrate.localization;
import java.io.*; import java.util.*; import java.text.*;
/** * <p>Title: Helper</p> * <p>Description: Converts files with messages to display from human-readable format to * midlet-readable format.</p> * <p>Copyright: Copyright Quadrate(c) 2003</p> * @author Quadrate * @version 1.0 */ public class Helper {
private final static String FILE_CHARACTER_SET = "UTF-8"; private final static String DELIMITER = "=";
/** * No instances */ private Helper() { }
public static void main(String[] args) { if (args.length != 2) { System.out.println("Illegal parameters. Please specify source file and destination file names."); } else { // Open in and out streams File sourceFile = new File(args[0]); File destFile = new File(args[1]); FileInputStream in = null; BufferedReader inReader = null; FileOutputStream out = null; DataOutputStream outWriter = null; try { // open source in = new FileInputStream(sourceFile); in.skip(3); inReader = new BufferedReader(new InputStreamReader (in, FILE_CHARACTER_SET)); // open destination out = new FileOutputStream(destFile); outWriter = new DataOutputStream(out);
Hashtable stringsTable = new Hashtable(); readSourceContent(inReader, stringsTable);
writeContent(outWriter, stringsTable); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } }
/** * Writes hashtable content into the stream specified. * @param outWriter * @param stringsTable * @throws IOException */ private static void writeContent(DataOutputStream outWriter, Hashtable stringsTable) throws IOException { for (Enumeration en = stringsTable.keys(); en.hasMoreElements();) { String key = (String) en.nextElement(); String value = (String) stringsTable.get(key); outWriter.writeUTF(key); outWriter.writeUTF(value); } }
/** * Reads and parses content of the source file. * @param inReader * @param stringsTable * @throws IOException */ private static void readSourceContent(BufferedReader inReader, Hashtable stringsTable) throws IOException, ParseException { String line = null; String prevKey = null; while ((line = inReader.readLine()) != null) { int delimiterPos = line.indexOf(DELIMITER); if (delimiterPos != -1) { String key = line.substring (0, delimiterPos); String value = line.substring (delimiterPos + 1); if (stringsTable.containsKey(key)) { if (prevKey.equals(key)) { StringBuffer buf = new StringBuffer((String) stringsTable.get(prevKey)); buf.append("\n").append(value); value = buf.toString(); } else { throw new ParseException("Duplicate key: " + key, 0); } } prevKey = key; stringsTable.put(key, value); System.out.println("key: " + key + "; value: " + value); } } } }
|
Заранее спасибо за помощь!
|