当前位置: 代码迷 >> Android >> 【高手帮帮忙】一个文件,放到指定的文件夹上
  详细解决方案

【高手帮帮忙】一个文件,放到指定的文件夹上

热度:51   发布时间:2016-05-01 13:00:36.0
【高手帮帮忙】一个文件,放到指定的文件夹下
DataOutputStream AllData = new DataOutputStream(new FileOutputStream(filename_date + ".bin"));
怎么把这个生成的文件放到SD卡自己指定的文件夹下面呢?
比如:
  这个文件生成在Android文件夹---->Data文件夹----->应用文件夹---->filename_date.bin;
我让它放到
  SD目录下----->我指定文件夹(如:Test文件夹)----->filename_date.bin

这个怎么实现呢? 
刚开始学习Android!谢谢了!!

------解决方案--------------------
public InputStream getInputStream(String path) throws FileNotFoundException{
FileInputStream filein = null;
filein = new FileInputStream(new File(path));
BufferedInputStream in = null;
if(filein != null)
in = new BufferedInputStream(filein);
return in;
}

/**
* 把输入流中的数据输入到Path里的文件里
* @param path
* @param fileName
* @param inputStream
* @return
*/
public File writeFromInputToSD(String path,InputStream inputStream){
File file = null;
OutputStream output = null;
try{
file = new File(path);
output = new FileOutputStream(file);
byte[] buffer = new byte[4*1024];
int temp;
while((temp=inputStream.read(buffer))!=-1){
output.write(buffer,0,temp);
}
output.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
return file;
}

实现的时候调用就行
String source = "......";
String to = "/SD/test/filename_date.bin";

writeFromInputToSD(to,getInputStream(source));

希望对你有帮助啦。。。自己去尝试吧。。。
------解决方案--------------------
你不是能生成文件么

File file = new File("你的sd卡+文件路径");
DataOutputStream AllData = new DataOutputStream(new FileOutputStream(file));
 
不就行了?
------解决方案--------------------
/**

* @param path传入路径字符串
* @return File
*/
public File creatFileIfNotExist(String path){
File file = new File(path);
if(!file.exists()){
try {
new File(path.substring(0, path.lastIndexOf(File.separator))).mkdirs();
file.createNewFile();
Log.i("creatFileIfNotExits", "Path:"+path+"creat success");
} catch (IOException e) {
e.printStackTrace();
Log.i("creatFileIfNotExits","Path:"+path+"&"+e.getMessage());
}
}
return file;
}


难道不能生成文件嘛,我这样子可是能在任何地方生成 文件的。
  相关解决方案