我想将c:\ttt.txt变成c:\\ttt.txt
帮我看看那而错了,本来想用split,但是编出来的程序老是有异常,改了一下还是不对.
package com.topfounder.eclipse;
import java.util.*;
import java.io.*;
public class JavaTest {


static String getFilePath(String s)
{
    int j=0;
String filePath=null;
ArrayList array=new ArrayList();
StringTokenizer st=new StringTokenizer(s.trim(),"\\");
while(st.hasMoreTokens())
{
array.add(st.nextElement());
}
for(int index=0;index<array.size()-1;index++)
{
filePath=filePath+array.get(index)+"\\";

}
filePath=filePath+array.get(array.size()-1);
return filePath; }
public static void main(String[] args)
{

String s=getFilePath("c:\ttt.txt");
System.out.println(s); }}

解决方案 »

  1.   

    import java.util.ArrayList;
    import java.util.StringTokenizer;/**
     * URL:http://community.csdn.net/Expert/topic/4689/4689753.xml?temp=.5499384
     * 
     * @author pangpang
     * @version 2006/04/16
     */
    public class JavaTest {    static String getFilePath(String s) {        String filePath = "";
            ArrayList<Object> array = new ArrayList<Object>();
            StringTokenizer st = new StringTokenizer(s.trim(), "\\");
            array.add(st.nextElement());
            while (st.hasMoreTokens()) {
                array.add(st.nextElement());
            }
            
            for (int index = 0; index < array.size()-1; index++) {
                filePath = filePath + array.get(index) + "\\\\";        }
            filePath = filePath + array.get(array.size() - 1);
            return filePath;
        }    public static void main(String[] args) {        String s = getFilePath("c:\\ttt.txt");
            System.out.println(s);    }}注意转义字符!!
      

  2.   

    但是我想实现将c:\ttt.txt变成c:\\ttt.txt,因为我数据库中存放的路径是c:\ttt.txt的格式
      

  3.   

    你可以将路径换成c:/ttt.txt这样不管怎么做都不用替换了。