其实就一个方法,就不贴所有代码了。

    /**
* 写入文件方法
* @param content
*/
public static void write(String content) {
try {
//判断实际是否有SD卡,且应用程序是否有读写SD卡的能力,有则返回true
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取SD卡的目录
File sdCardDir = Environment.getExternalStorageDirectory();
String path = "/APP/";
File dir = new File(sdCardDir+path);
if (!dir.exists()) {
dir.mkdirs();
}
File targetFile = new File(sdCardDir.getCanonicalPath() + path+"aaa.txt");
//使用RandomAccessFile是在原有的文件基础之上追加内容,
//而使用outputstream则是要先清空内容再写入
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
//光标移到原始文件最后,再执行写入
raf.seek(targetFile.length());
raf.write(content.getBytes());
raf.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
package com.example.linfeng.myapplication;

import android.os.Environment;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by LinFeng on 2017/11/13.
*/ public class TextHelper { public static void write(String content, String PathName, String FileName) {
try {
//判断实际是否有SD卡,且应用程序是否有读写SD卡的能力,有则返回true if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取SD卡的目录
File sdCardDir = Environment.getExternalStorageDirectory();
//Log.i("paht",sdCardDir.toString());
String path = "/" + PathName + "/";
//如果不存在,就创建目录
File dir = new File(sdCardDir + path);
if (!dir.exists()) {
dir.mkdirs();
}
File targetFile = new File(sdCardDir.getCanonicalPath() + path + FileName + ".dat");
//使用RandomAccessFile是在原有的文件基础之上追加内容,
//而使用outputstream则是要先清空内容再写入
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
//光标移到原始文件最后,再执行写入
raf.seek(targetFile.length());
raf.write(content.getBytes());
raf.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} //写头文件,如果不存在写,存在就不写
public static void writeHead(String path,String fileName,char type){ String string=Environment.getExternalStorageDirectory()+"/" + path + "/"+fileName+".dat";
if (!fileIsExists(string)){
switch (type){
case 'I':
String content = "Type=Inventory"+"\r\n"+"Date="+getStringDate()+"\r\n"+"ScannerNo=01";
write(content,path,fileName);
break;
case 'M':
write("Type=Market"+"\r\n",path,fileName);
break;
case 'R':
write("Type=Return"+"\r\n",path,fileName);
break;
}
}else{ }
} //判断文件是否存在
public static boolean fileIsExists(String strFile) {
try {
File f = new File(strFile);
if (!f.exists()) {
return false;
}
} catch (Exception e) {
return false;
}
return true;
} //获取当前时间,格式为:yyyyMMdd HH:mm:ss
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
}

安卓如何将TXT文件写到特定路径的更多相关文章

  1. 读取同一文件夹下多个txt文件中的特定内容并做统计

    读取同一文件夹下多个txt文件中的特定内容并做统计 有网友在问,C#读取同一文件夹下多个txt文件中的特定内容,并把各个文本的数据做统计. 昨晚Insus.NET抽上些少时间,来实现此问题,加强自身的 ...

  2. Python读写txt文件时的编码问题

    这个问题来自于一个小伙伴,他在处理中文数据时需要先把里面的文本过滤然后分词,因为里面有许多符号,不仅是中文标点符号,还有✳,emoji等奇怪的符号. 正常情况下,中文的str经过encode('utf ...

  3. MySQL笔记(三)由txt文件导入数据

    改编自学校实验,涉及一些字符集相关的问题. 索引 建库 导入数据 最终脚本 下载数据 点击这里 建库 create.sql DROP DATABASE IF EXISTS orderdb; CREAT ...

  4. pipreqs 生成requirements.txt文件时编码错误问题

    1,首先安装pipreqs --> pip install pipreqs 2.生成相应项目的路径  --> pipreqs  e:\a\b 在此时可能会遇见 UnicodeDecodeE ...

  5. 使用Java将搜狗词库文件(文件后缀为.scel)转为.txt文件

    要做一个根据词库进行筛选主要词汇的功能,去搜狗下载专业词汇词库时,发现是.scel文件,且通过转换工具(http://tools.bugscaner.com/sceltotxt/)转换为txt时报错如 ...

  6. 【学习总结】GirlsInAI ML-diary day-15-读/写txt文件

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day15 认识读/写txt文件 路径: 绝对路径:文件在电脑中的位置 相对路径:下面会用到 1-准备 新建一个 pytho ...

  7. java 写一个"HelloJavaWorld你好世界"输出到操作系统文件Hello.txt文件中

    package com.beiwo.homework; import java.io.File; import java.io.FileOutputStream; import java.io.IOE ...

  8. C# 读取网络txt文件 并写在本地txt文件中

    public void WriteStream() { try { stirng ImagesPath = ImagesPath + "\\2013-09-27.txt"; Htt ...

  9. 在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间

    在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间

随机推荐

  1. XML文件解析-DOM4J方式和SAX方式

    最近遇到的工作内容都是和xml内容解析相关的. 1图片数据以base64编码的方式保存在xml的一个标签中,xml文件通过接口的方式发送给我,然后我去解析出图片数据,对图片进行进一步处理. 2.xml ...

  2. Spring Boot入门——thymeleaf模板使用

    使用步骤 1.在pom.xml中引入thymeleaf <!-- thymeleaf插件 --> <dependency> <groupId>org.springf ...

  3. js的事件处理与闭包:

    var i = 0; for(i=0;i<5;i++){ (function(i){ setTimeout(function(){alert(i)},3000); })(i) } // 上面打印 ...

  4. Binding RelativeSource

    IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}&q ...

  5. Hive数据分析——Spark是一种基于rdd(弹性数据集)的内存分布式并行处理框架,比于Hadoop将大量的中间结果写入HDFS,Spark避免了中间结果的持久化

    转自:http://blog.csdn.net/wh_springer/article/details/51842496 近十年来,随着Hadoop生态系统的不断完善,Hadoop早已成为大数据事实上 ...

  6. LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. http请求 详解

  8. 滑雪(经典DP思想)

    个人心得:思想还是不够,开始自己写但是不知道如何记录长度,也不太知道状态的转移,后面看了百度, 发现人人为我我为人人就是一步一步推导, 而递归思想就要求学会记录和找到边界条件,这一题中的话就是用递归, ...

  9. LeetCode 369. Plus One Linked List

    原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...

  10. LeetCode 251. Flatten 2D Vector

    原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...