import
java.io
.File;
import java.io
.FileInputStream;
import java.io
.FileNotFoundException;
import java.io
.FileOutputStream;
import java.io
.FileReader;
import java.io
.FileWriter;
import java.io
.IOException;
import java.io
.UnsupportedEncodingException;
public class IODemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//File的使用
File file = new File ("E:/a.txt");
System.out.println(file.length());
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.getParent());
File aa = new File("E:/aa");
if(aa.mkdir()){
System.out.println("aa创建成功....");
}
FileInputStream fis = null;
try{
fis = new FileInputStream("E:/a.txt");
byte[] buff = new byte[(int)file.length()];
int readbyte = fis.read(buff);
for (int i = 0; i < buff.length; i++) {
System.out.println(buff[i]);
}
String str = new String(buff);
System.out.println(str);
}
catch (FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fis = null;
}
String s1 = "这是输出的数据 1234567890qwertyuiopasdfghjklzxcvbnm";
byte[] sbuff = null;
FileOutputStream fos = null;
try {
sbuff = s1.getBytes("utf8");
fos = new FileOutputStream("E:/at.txt");
fos.write(sbuff);
fos.flush();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fos = null;
}
FileReader fr = null;
try {
fr = new FileReader("E:/a.txt");
char ch[] = new char[100];
int len = fr.read(ch);
for (int i = 0; i < len; i++) {
System.out.println(ch[i]);
}
String ss = new String(ch,0,len);
System.out.println(ss);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fr = null;
}
FileWriter fw = null;
try {
fw = new FileWriter("E:/at2.txt");
fw.write(s1);
fw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fw = null;
}
}
}
.File;
import java.io
.FileInputStream;
import java.io
.FileNotFoundException;
import java.io
.FileOutputStream;
import java.io
.FileReader;
import java.io
.FileWriter;
import java.io
.IOException;
import java.io
.UnsupportedEncodingException;
public class IODemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//File的使用
File file = new File ("E:/a.txt");
System.out.println(file.length());
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.getParent());
File aa = new File("E:/aa");
if(aa.mkdir()){
System.out.println("aa创建成功....");
}
FileInputStream fis = null;
try{
fis = new FileInputStream("E:/a.txt");
byte[] buff = new byte[(int)file.length()];
int readbyte = fis.read(buff);
for (int i = 0; i < buff.length; i++) {
System.out.println(buff[i]);
}
String str = new String(buff);
System.out.println(str);
}
catch (FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fis = null;
}
String s1 = "这是输出的数据 1234567890qwertyuiopasdfghjklzxcvbnm";
byte[] sbuff = null;
FileOutputStream fos = null;
try {
sbuff = s1.getBytes("utf8");
fos = new FileOutputStream("E:/at.txt");
fos.write(sbuff);
fos.flush();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fos = null;
}
FileReader fr = null;
try {
fr = new FileReader("E:/a.txt");
char ch[] = new char[100];
int len = fr.read(ch);
for (int i = 0; i < len; i++) {
System.out.println(ch[i]);
}
String ss = new String(ch,0,len);
System.out.println(ss);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fr = null;
}
FileWriter fw = null;
try {
fw = new FileWriter("E:/at2.txt");
fw.write(s1);
fw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fw = null;
}
}
}