utfencodertool.java.svn-base
来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 151 行
SVN-BASE
151 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program 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. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me.utils;import com.sun.tck.cldc.javatest.util.UTFConverter;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStream;import java.io.StreamTokenizer;import java.util.ArrayList;import java.util.List;/** * Command line tool for converting plain text files to UTF encoded files. * To be used for building files like <code>agent.dat</code> from editable * sources. * <p> * Usage: * <code> * java -cp j2mefw_jt.jar com.sun.tck.j2me.utils.UTFEncoderTool * -in <input_file> -out <output_file> * </code> * <p> * Breaks the input into sequence of words delimited by whitespaces, * then builds an array of <code>String</code> from this sequence and encodes it * using {@link com.sun.tck.cldc.javatest.util.UTFConverter}. */public class UTFEncoderTool { /** Creates a new instance of UTFEncoderTool */ public UTFEncoderTool(String[] args) { decodeArgs(args); } public static void main(String[] args) { new UTFEncoderTool(args).run(); } public void run() { String[] args; try { args = readStrings(plainFile); } catch (Exception e) { throw new RuntimeException("Error reading input file " + plainFile.getPath() + ": " + e.getMessage(), e); } try { encodeAndWrite(args, encodedFile); } catch (Exception e) { throw new RuntimeException("Error writing output file " + encodedFile.getPath() + ": " + e.getMessage(), e); } } private String[] readStrings(File inFile) throws IOException { FileReader reader = null; try { StreamTokenizer in; List<String> args = new ArrayList<String>(); reader = new FileReader(inFile); in = new StreamTokenizer(reader); in.resetSyntax(); in.whitespaceChars(0, 32); in.wordChars(33, '\uFFFF');readInputLoop: while (true) { in.nextToken(); switch (in.ttype) { case StreamTokenizer.TT_EOF: break readInputLoop; case StreamTokenizer.TT_WORD: args.add(in.sval); break; default: throw new RuntimeException("Invalid input file: " + inFile.getPath()); } } return args.toArray(new String[args.size()]); } finally { if (reader != null) { reader.close(); } } } private void encodeAndWrite(String[] args, File outFile) throws IOException { OutputStream out = null; try { out = new FileOutputStream(outFile, /* append */ false); UTFConverter.stringsToUTFStream(args, out); } finally { if (out != null) { out.close(); } } } private void decodeArgs(String[] args) { for (int i = 0; i < args.length; i++) { if (args[i].equals("-in") && (i + 1 < args.length)) { plainFile = new File(args[++i]); } else if (args[i].equals("-out") && (i + 1 < args.length)) { encodedFile = new File(args[++i]); } } if (plainFile == null) { throw new RuntimeException("Must specify input plain file with " + "-in argument"); } if (encodedFile == null) { throw new RuntimeException("Must specify output UTF-8 encoded file" + " with -out argument"); } } private File plainFile; private File encodedFile;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?