androidurl文件路径怎么写

1.android开发 通过URL返回文件并保存到指定的SD卡路径中public static void writeFileData(String fileName, String message){
try{
FileOutputStream output = new FileOutputStream("/sdcard/" + fileName);
byte[] bytes = message.getBytes();
output.write(bytes);
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
自己指定文件名,写文件就可以了 。
2.android的项目里怎么规定文件路径的方法一:把目标文件放入resources文件中,以通过读取R的资源文件来获取,具体方式如下:1、在res下新建raw文件,将带读取文件添加到raw文件目录下 。
2、添加如下代码:// 如果要使用文件名获取文件数据:首先获取资源id然后再通过id获取输入流 InputStream im = getResources().openRawResource(R.raw.h_data11); BufferedReader read = new BufferedReader(new InputStreamReader(im)); String line = ""; StringBuilder sb = new StringBuilder(); try { while((line = read.readLine()) != null) { sb.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { if(read != null) { try { read.close(); read = null; } catch (IOException e) { e.printStackTrace(); } }if(im != null) { try { im.close(); im = null; } catch (IOException e) { e.printStackTrace(); } } } Log.v("", "result = " + sb.toString());方法二:使用assets 只读文件进行读取 。1、将文件copy到assets下,可以新建文件夹如:“www”然后将文件放入www文件夹中,读取的path为:"www/filename"String result = "";ObjectInputStream ois = null; AssetManager am = context.getResources().getAssets(); try { ois = new ObjectInputStream(am.open("www/filename")); result = (String) ois.readObject(); } catch ( e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch ( e) { e.printStackTrace(); } finally { try { if (ois != null) { ois.close(); ois = null; } } catch (IOException e) { e.printStackTrace(); } }以对象的方式读取文件中的数据,如果没有新建文件夹,把前面的“www/”去掉就ok啦以上方式我都还有疑问的地方:1、raw下新建多级目录是否真的不能够使用 。
【androidurl文件路径怎么写】

androidurl文件路径怎么写

文章插图