try BufferedReader,BufferedWriter

解决方案 »

  1.   

    瓶径在哪?
    应该是:
    1.16到2的转换。
    2.文件写入。
    如何解决?
    1.转换不要自己写函数,用java内部提供的。
    2.文件写入用带缓冲区的,而且把缓冲区设到最大。
      

  2.   

    1,用缓存,读入的数据怎么转换?我觉得读入的数据无论怎么存,还是需要拼接、转换的过程,缓存我也用过,性能差不多,可能是我文件比较小的原因。这个东西的瓶颈在大量对象的创建以及频繁的运算上面,比如new String(或者StringBuffer的清空),频繁的Integer.parseInt运算。这是我的理解,不知道有没有错误,请指教。另外,不是16到2的转换,而是字符表示的16进制数字(如new String("40")) 到实际的16进制数字(Integer.parseInt("40",16))之间的转换。
    2,文件写入,我是要写入int,不是文本,假使设缓冲区一次写入,还是存在将int存入数组的开销,另外,数组开多大?因为废数据("\n\r")的存在(文件中有"0D0A"需要忽略掉),是不可预知的。
    3、用多线程,我可以试试,但是我觉得满负荷工作的代码段分成多线程也不一定能起色多少,无论怎么样,试试再说,呵呵,明天如果没有其他办法就结贴了,大家畅所欲言阿..
    谢谢大家 :P
      

  3.   


    import java.io.*;public class Transfer {
        public static void main(String[] arg) {
            long st = System.currentTimeMillis();
            run();
            long ed = System.currentTimeMillis();
            System.out.println("Time=" + (ed - st));
        }    static void run() {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(
                        new FileInputStream("input.txt"));
                bos = new BufferedOutputStream(
                        new FileOutputStream("output.txt"));            byte[] bs = new byte[2];
                int ret = bis.read(bs);
                while (ret != -1) {
                    if (bs[0] == 13 && bs[1] == 10) {
                    } else {
                        if (bs[0] >= 65 && bs[0] <= 70) {
                            bs[0] = (byte) (bs[0] - 7);
                        }
                        if (bs[1] >= 65 && bs[1] <= 70) {
                            bs[1] = (byte) (bs[1] - 7);
                        }
                        byte bb = (byte) ((bs[0] << 4) | (bs[1] & 15));
                        bos.write(bb);
                    }
                    ret = bis.read(bs);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                    bis.close();
                } catch (IOException ee) {
                }
            }
        }
    }
      

  4.   

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;public class Converter
    {
    private static final byte[] DECODE_TABLE = new byte[256]; static
    {
    for(char c = '0'; c <= '9'; c++)
    DECODE_TABLE[c] = (byte) (c - '0');
    for(char c = 'a'; c <= 'f'; c++)
    DECODE_TABLE[c] = (byte) (c - 'a' + 10);
    for(char c = 'A'; c <= 'F'; c++)
    DECODE_TABLE[c] = (byte) (c - 'A' + 10);
    } public static void convert(InputStream in, OutputStream out) throws IOException
    {
    byte[] buf = new byte[2];
    int bytes = 0;
    while((bytes = in.read(buf)) == 2)
    out.write((DECODE_TABLE[buf[0]] << 4) + DECODE_TABLE[buf[1]]);
    if(bytes == 1)
    out.write(DECODE_TABLE[buf[0]]);
    out.flush();
    } public static void main(String[] args)
    {
    try
    {
    FileInputStream fis = new FileInputStream("in");
    FileOutputStream fos = new FileOutputStream("out");
    convert(new BufferedInputStream(fis, 512), new BufferedOutputStream(fos, 512));
    fis.close();
    fos.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    System.exit(1);
    }
    }
    }
      

  5.   

    不知道 BigInteger 这个东西行不行,速度快不快 ……
    import java.math.*;public class ParseInt
    {

    // usre BigInteger()
    public static void main (String[] args)
    {
    if (args.length != 2)
    {
    System.out.println ("Usage: java ParseInt <待分析的字符串> <什么进制>");
    return;
    }

    int radix = Integer.parseInt (args[1]);

    System.out.println (
    new BigInteger (args[0], radix) );
    }
    }------------------------
    D:\Test\Java>java ParseInt
    Usage: java ParseInt <待分析的字符串> <什么进制>D:\Test\Java>java ParseInt 4040C3E3C9F1F2F84040404040404040404040404040404040404
    04040404040404040404040404040404040F30550C8C4D9F1C1C2C3C440404040404040404040404
    040404040404040F0F0F0F1F0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F
    0C1F04040404040404040404040C3E3C9F1F2F840404040404040404040404040404040404040404
    0404040404040404040404040404040F30550C8C4D9F1C1C2C3C4404040404040404040404040404
    04040404040F0F0F0F1F0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F
    04040404040404040404040C3E3C9F1F2F8404040404040404040404040404040404040404040404
    040404040404040404040404040F30550C8C4D9F1C1C2C3C44040404040404040404040404040404
    0404040F0F0F0F1F0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F0404
    0404040404040404040C3E3C9F1F2F84040404040404040404040404040404040404040404040404
    04040404040404040404040F30550C8C4D9F1C1C2C3C440404040404040404040404040404040404
    040F0F0F0F1F0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F04040404
    040404040404040C3E3C9F1F2F840404040404040404040404040404040404040404040404040404
    0404040404040404040F30550C8C4D9F1C1C2C3C440404040404040404040404040404040404040F
    0F0F0F1F0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F040404040404
    04040404040C3E3C9F1F2F8404040404040404040404040404040404040404040404040404040404
    040404040404040F30550C8C4D9F1C1C2C3C440404040404040404040404040404040404040F0F0F
    0F1F0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F0404040404040404
    0404040C3E3C9F1F2F84040404040404040404040404040404040404040404040404040404040404
    04040404040F30550C8C4D9F1C1C2C3C440404040404040404040404040404040404040F0F0F0F1F
    0F0F0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F04040404040404040404
    040C3E3C9F1F2F840404040404040404040404040404040404040404040404040404040404040404
    0404040F30550C8C4D9F1C1C2C3C440404040404040404040404040404040404040F0F0F0F1F0F0F
    0F1F0F0F0F1F0F040F9F9F2F1F940F0F0F2F1F940F0F0F0F0F0F0C1F04040404040404040404040C
    3E3C9F1F2F8404040404040404040404040404040404040404040404040404040404040404040404
    040F30550C8C4D9F1C1C2C3C4404040404040404040404040404040 16
    22645306163634803413346421030385762777437935553739606751412093027073264663460180
    93178655622478132436615407257080028062242192581296432653460712514153952942641078
    83983378290024168172153290240050843995115302906535250012946801334065545844046446
    41225389712741377191285101345309309446617515052922069142701422887057951295218180
    29772904934053511556396725081575380023051891281684230906739919797954243190245520
    57649226718797427920074981674637555582668294694498936151073302718984734184061573
    91522001447938482620937466410924688074126884763320310380545472440327470653749474
    75060267119636373744402421706255786618726006429907296172714552404501960907978456
    16980593639579894631181781638329070805228406743429992293174689181348044872371054
    50543758063865856020327323554530654712829726941745918526743813527169790492090177
    29637341126380913024079649096616752982627801705385196298305577943612717979700250
    71474573697319892998942125617205942049176374337784767706959835395293620165890335
    58391338138974123376760230531589663305714448374541356690352382937334462637443734
    99401502265605008056948296073945140084230069162193144210576105229783019792763672
    09653958855976301385315551093723776281243885448752588243201198299703088376975109
    86040602884752451435184815933641019540899468533714400561449995771250842636703796
    07953380916310793323596845854886581813369417202741861213113776343007637114409756
    43873166325530768763809314796860290736054641990723082380292176883944144404133030
    54490016931458003563782681775169926348764412609807061935314356947895191368463664
    13733118486292849971614958451513297879100052453720551673755802428749806560232522
    36814849609751964882346938665827639665139384074449397425634797040011480385105487
    80325424875600039219865254654188789525930309866677090027759827380689494265611059
    90428714926512184106587853618799112626031097110994400628594542382457027972817943
    54244890095368579766063166235211422369568752456587417847832058950413273966441300
    15452007173932607326182825823961329538972422578008790914195996294249600771327115
    91472984569792293489998646077304205603587142598121165093125417932238191932171476
    93709014844568081305866147618058202847027834453696006052728732404054154924974580
    10134805868116256751833719132632110254864261599604585740631620577871450466170809
    73898222888160238273494895998995694485712316550563335992402350598694368951675089
    10696118568448535909017931207251421320313334902234818499018153734920852811599954
    947696494317168014229951901560540644786240
      

  6.   

    更快一点的convert方法: private static byte[] inBuffer = new byte[512];
    private static byte[] outBuffer = new byte[inBuffer.length / 2]; public static void convert(InputStream in, OutputStream out) throws IOException
    {
    int bytes = 0;
    while((bytes = in.read(inBuffer)) > 0)
    {
    int full = (bytes % 2 == 0) ? bytes : bytes - 1;
    int inIndex = 0;
    int outIndex = 0;
    while(inIndex < full)
    outBuffer[outIndex++] = (byte) ((DECODE_TABLE[inBuffer[inIndex++]] << 4) | DECODE_TABLE[inBuffer[inIndex++]]);
    out.write(outBuffer, 0, outIndex);
    if(full != bytes)
    out.write(DECODE_TABLE[inBuffer[inIndex]]);
    }
    out.flush();
    }
      

  7.   

    在我的机器(P4 2G)上转换246321字节的文件平均20ms