篇首语:本文由小编为大家整理,主要介绍了Android写入txt文件相关的知识,希望对你有一定的参考价值。
public class MainActivity extends Activity
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=new TextView(this);
String myString =null;
try
URL myURL=new URL("http://172.17.12.81/helloworld?key=0");
URLConnection ucon = myURL.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1)
baf.append((byte)current);
myString = EncodingUtils.getString(baf.toByteArray(), "UTF-8");
catch(Exception e)
myString = e.getMessage();
通过URLConnection读取到的数据,怎样才能写入到本地的txt文件中呢?
分以下几个步骤:
首先对manifest注册SD卡读写权限
androidManifest.xmlandroid:versionName="1.0" >
android:targetSdkVersion="16" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:label="@string/app_name" >
创建一个对SD卡中文件读写的类
FileHelper.java/**
* @Title: FileHelper.java
* @Package com.tes.textsd
* @Description: TODO(用一句话描述该文件做什么)
* @author Alex.Z
* @date 2013-2-26 下午5:45:40
* @version V1.0
*/
package com.tes.textsd;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;
public class FileHelper
private Context context;
/** SD卡是否存在**/
private boolean hasSD = false;
/** SD卡的路径**/
private String SDPATH;
/** 当前程序包的路径**/
private String FILESPATH;
public FileHelper(Context context)
this.context = context;
hasSD = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
SDPATH = Environment.getExternalStorageDirectory().getPath();
FILESPATH = this.context.getFilesDir().getPath();
/**
* 在SD卡上创建文件
*
* @throws IOException
*/
public File createSDFile(String fileName) throws IOException
File file = new File(SDPATH + "//" + fileName);
if (!file.exists())
file.createNewFile();
return file;
/**
* 删除SD卡上的文件
*
* @param fileName
*/
public boolean deleteSDFile(String fileName)
File file = new File(SDPATH + "//" + fileName);
if (file == null || !file.exists() || file.isDirectory())
return false;
return file.delete();
/**
* 写入内容到SD卡中的txt文本中
* str为内容
*/
public void writeSDFile(String str,String fileName)
try
FileWriter fw = new FileWriter(SDPATH + "//" + fileName);
File f = new File(SDPATH + "//" + fileName);
fw.write(str);
FileOutputStream os = new FileOutputStream(f);
DataOutputStream out = new DataOutputStream(os);
out.writeShort(2);
out.writeUTF("");
System.out.println(out);
fw.flush();
fw.close();
System.out.println(fw);
catch (Exception e)
/**
* 读取SD卡中文本文件
*
* @param fileName
* @return
*/
public String readSDFile(String fileName)
StringBuffer sb = new StringBuffer();
File file = new File(SDPATH + "//" + fileName);
try
FileInputStream fis = new FileInputStream(file);
int c;
while ((c = fis.read()) != -1)
sb.append((char) c);
fis.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return sb.toString();
public String getFILESPATH()
return FILESPATH;
public String getSDPATH()
return SDPATH;
public boolean hasSD()
return hasSD;
写一个用于检测读写功能的的布局
main.xmlandroid:layout_
android:layout_
android:text="hello" />
android:layout_
android:layout_
android:text="hello" />
android:layout_
android:layout_
android:text="hello" />
android:layout_
android:layout_
android:text="false" />
android:layout_
android:layout_
android:text="false" />
android:layout_
android:layout_
android:text="false" />
就是UI的类了
FileOperateActivity.class/**
* @Title: FileOperateActivity.java
* @Package com.tes.textsd
* @Description: TODO(用一句话描述该文件做什么)
* @author Alex.Z
* @date 2013-2-26 下午5:47:28
* @version V1.0
*/
package com.tes.textsd;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class FileOperateActivity extends Activity
private TextView hasSDTextView;
private TextView SDPathTextView;
private TextView FILESpathTextView;
private TextView createFileTextView;
private TextView readFileTextView;
private TextView deleteFileTextView;
private FileHelper helper;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hasSDTextView = (TextView) findViewById(R.id.hasSDTextView);
SDPathTextView = (TextView) findViewById(R.id.SDPathTextView);
FILESpathTextView = (TextView) findViewById(R.id.FILESpathTextView);
createFileTextView = (TextView) findViewById(R.id.createFileTextView);
readFileTextView = (TextView) findViewById(R.id.readFileTextView);
deleteFileTextView = (TextView) findViewById(R.id.deleteFileTextView);
helper = new FileHelper(getApplicationContext());
hasSDTextView.setText("SD卡是否存在:" + helper.hasSD());
SDPathTextView.setText("SD卡路径:" + helper.getSDPATH());
FILESpathTextView.setText("包路径:" + helper.getFILESPATH());
try
createFileTextView.setText("创建文件:"
+ helper.createSDFile("test.txt").getAbsolutePath());
catch (IOException e)
e.printStackTrace();
deleteFileTextView.setText("删除文件是否成功:"
+ helper.deleteSDFile("xx.txt"));
helper.writeSDFile("1213212", "test.txt");
readFileTextView.setText("读取文件:" + helper.readSDFile("test.txt"));
FileOutputStream fs = new FileOutputStream("a.txt",true);
再写入你的内容就行了,不用我说具体怎么做了吧,能写程序到这样,后面你肯定知道的了本回答被提问者采纳
java 怎么将数据写入TXT文件
怎么将int数组数据写入TXT文件 例如将int A[20];中的20个数据写入result.txt文件中
求大虾告诉我关键几句代码
定义一个输出文件,然后输出就可以了,具体见下面的代码
import java.io.*;public class StreamDemo
public static void main(String args[])
File f = new File("c:\\temp.txt") ;
OutputStream out = null ;
try
out = new FileOutputStream(f) ;
catch (FileNotFoundException e)
e.printStackTrace();
// 将字符串转成字节数组
byte b[] = "Hello World!!!".getBytes() ;
try
// 将byte数组写入到文件之中
out.write(b) ;
catch (IOException e1)
e1.printStackTrace();
try
out.close() ;
catch (IOException e2)
e2.printStackTrace();
// 以下为读文件操作
InputStream in = null ;
try
in = new FileInputStream(f) ;
catch (FileNotFoundException e3)
e3.printStackTrace();
// 开辟一个空间用于接收文件读进来的数据
byte b1[] = new byte[1024] ;
int i = 0 ;
try
// 将b1的引用传递到read()方法之中,同时此方法返回读入数据的个数
i = in.read(b1) ;
catch (IOException e4)
e4.printStackTrace();
try
in.close() ;
catch (IOException e5)
e5.printStackTrace();
//将byte数组转换为字符串输出
System.out.println(new String(b1,0,i)) ;
参考技术A import java.io.FileWriter;
import java.io.IOException;
public class Test02
void writefile() throws IOException
FileWriter fileWriter=new FileWriter("c:\\Result.txt");
int [] a=new int[]111,222,333,444,555,666;
for (int i = 0; i < a.length; i++)
fileWriter.write(String.valueOf(a[i])+" ");
fileWriter.flush();
fileWriter.close();
public static void main(String[] args) throws IOException
new Test02().writefile();
//你看看,就这两句,测试通过了!本回答被提问者采纳 参考技术B I/O流
PrintWriter实现 参考技术C 用FileWriter
file f = new file(filename);
FileWriter fw=new FileWriter(f);
fw.write(new String(A,0,20)); 参考技术D 回答的真好。
不用补充了。
以上是关于Android写入txt文件的主要内容,如果未能解决你的问题,请参考以下文章