标签:tin 长度 name 疑问 view ring 否则 file 信息
String pathName = "C:\\Users\\dml\\Desktop\\test.txt";
File file = new File(pathName);
//利用多态性实例化outputStream,参数2位append,意指是否允许追加内容
//当设置为true时,每执行一次,都会向文件中写入一次内容
//否则只会覆盖上一次的输出
OutputStream outputStream = new FileOutputStream(file,true);
//要输出的内容
String outPutInfo = "test";
//将要输出的内容转化为字节
byte[] outputInfoByte = outPutInfo.getBytes();
//向外输出
outputStream.write(outputInfoByte);
//关闭流,谨记!必须关闭
outputStream.close();
---------------------------------------------------------------
以上代码是出入流的操作及其介绍
public static void main(String[] args) throws IOException{
String pathName = "C:\\Users\\dml\\Desktop\\test.txt";
File file = new File(pathName);
//利用多态性实例化readerInfo
Reader readerInfo = new FileReader(file);
//设置一个接收输入信息的字符数组,容量为1024;
char[] readerInfochar = new char[1024];
//readerInfochar最终存储信息的字符长度
int len = 0;
//输入的信息,当=-1时,表示全部输入
int temp = 0;
while ((temp=readerInfo.read()) != -1) {
//将输入的信息重新转化会char
readerInfochar[len]=(char) temp;
len++;
}
//关闭流,谨记!!
readerInfo.close();
//将输入的内容转化成String
String input = new String(readerInfochar,0,len);
System.out.println(input);
}
---------------------------------------------------------------
以上是读取数据的方法 在java中可以直接显示 android的话 需要个显示位置 比如:toast,一个textview
public static void main(String[] args) throws IOException{
String pathName = "C:\\Users\\dml\\Desktop\\test.txt";
File file = new File(pathName);
//实例化outputStream,构造方法需要内接一个基本流
DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file)); //模拟的测试信息
String outInfo = "outputStream";
double test = 99.8;
int num =3;
//输出信息
outputStream.writeChars(outInfo);
outputStream.writeChar(‘\n‘);
outputStream.writeDouble(test);
outputStream.writeChar(‘\n‘);
outputStream.writeInt(num);
outputStream.writeChar(‘\n‘);
//关闭流,谨记
outputStream.close();
}
------------------------------------------- 以上是 高级的输入流
public static void main(String[] args) throws IOException{
String pathName = "C:\\Users\\dml\\Desktop\\test.txt";
File file = new File(pathName);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new
FileOutputStream(file),"utf-8");
String outString = "以utf-8格式进行编码的文件";
outputStreamWriter.write(outString);
outputStreamWriter.close();
}
-------------------------------------------------
以上是高级输入流
如有疑问 请留言 有好的文章 也可以分享
标签:tin 长度 name 疑问 view ring 否则 file 信息
原文地址:http://www.cnblogs.com/hmy550370001/p/6889927.html