62,614
社区成员
发帖
与我相关
我的任务
分享
package IO;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* test.getBytes("iso8859-1"))
* 用OutputStream和InputStream实现文件的写入和读取(字节的写入和读取)
*
*/
public class InAndOut {
String path="e:\\abc.txt";
public static void main(String[] args) {
InAndOut io=new InAndOut ();
io.write("我是邪恶少年!");
io.read();
}
public void write(String content)
{
File f=new File (path);
boolean add=true;//是否写入
try {
if (f.exists()==false) {
f.createNewFile();
FileOutputStream os=new FileOutputStream (f);//不用判断是否写入
//FileOutputStream os=new FileOutputStream (f,add);//要判断是否写入
os.write(content.getBytes());
os.flush();
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read()
{
try {
InputStream is=new FileInputStream(path);
//BufferedInputStream bis=new BufferedInputStream(new FileInputStream(path));
int temp=0;
String test="";
try {
while ((temp=is.read())!=-1) {
test+=(char)temp;
}
System.out.println(new String (test.getBytes("iso8859-1")));//会出现乱码
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package IO;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
/**
*
*用read和write实现文件的写入和读取(字符的写入和读取)
*
*/
public class ReadAndWrite {
//定义文件的路径,一边写入和读取使用
String path="e:\\abc.txt";
public static void main(String[] args) {
ReadAndWrite r=new ReadAndWrite ();
r.writeFile("我乃邪恶少年是也!");
r.readFile();
}
private void readFile() {
// TODO Auto-generated method stub
try {
Reader r=new BufferedReader (new FileReader(path));
String test="";
int temp=0;
try {
while ((temp=r.read())!=-1) {
test+=(char)temp;
}
System.out.println(test);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writeFile(String content) {
// TODO Auto-generated method stub
File f=new File (path);
if (f.exists()==false) {
try {
f.createNewFile();
FileWriter fw=new FileWriter (f);
fw.write(content);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
System.out.println(path+"已存在,写入失败,源文件是");
}
}
}
package com.wsb.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadyFile {
/**
* @param args
*/
public static void main(String[] args) {
//定义一个固定大小的字节数组长度为2
byte[] by = new byte[2];
try {
//创建一个文件对象用于去读取文件对象
File file = new File("D:/3.txt");
//定义一个字节读取流对文件进行读取
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()]; //定义一个与文件字节长度一样大小的字节数组
int i = 0 ; //记录字节数据保存到了哪一个下标
while((fis.read(by)) > 0){
for(int j = 0 ; j < by.length; j ++,i++){
try{
b[i] = by[j]; //将读取到的字节保存到字节数组当中
//由于当前定义每次去读取的字节数组的大小固定,所以有可能超出下标
}catch(ArrayIndexOutOfBoundsException e){
}
}
}
System.out.println(new String(b));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
1啊bnsns你好!