日期:2014-05-20  浏览次数:20705 次

j2me 创建多级文件夹方法

这个方法是创建多层文件夹的一个方法,写的有点笨拙,或许用递归能够好些,不过递归恐怕会拖机器~

?

上代码

public void careateFconn(String deviceUrl)
	{
		FileConnection fconn = null;
		String str_headUrl = deviceUrl.substring(0, deviceUrl.indexOf(":/")) + ":/";
		String str_lowUrl = deviceUrl.substring(deviceUrl.indexOf(":/") + 1);

		int temp = 0;
		Vector v = new Vector(0, 1);
		while((temp = str_lowUrl.indexOf("/")) != -1)
		{
			v.addElement(str_lowUrl.substring(0, temp));
			str_lowUrl = str_lowUrl.substring(temp+3);
		}
		for(int i = 0; i < v.size(); i++)
		{
			try 
			{
				fconn = (FileConnection) Connector.open(str_headUrl + v.elementAt(i).toString(), Connector.READ_WRITE);
				if(!fconn.exists())
					fconn.mkdir();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			} 
			finally 
			{
				try 
				{
					fconn.close();
				}
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		}
	}

?

未经过仔细测试,如有问题请留言。