大家帮我看看程序那儿错了

tondayong1981 2006-04-15 11:17:15
我想将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);

}

}
...全文
253 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
btb368 2006-04-17
  • 打赏
  • 举报
回复
你可以将路径换成c:/ttt.txt这样不管怎么做都不用替换了。
tondayong1981 2006-04-16
  • 打赏
  • 举报
回复
但是我想实现将c:\ttt.txt变成c:\\ttt.txt,因为我数据库中存放的路径是c:\ttt.txt的格式
interpb 2006-04-16
  • 打赏
  • 举报
回复
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);

}

}

注意转义字符!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧