public class BufferedReaderDemo {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader
(new FileInputStream("d:/heihei.txt")));
String text = br.readLine();
System.out.println(text);
br.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
public class CopyDemo01 {
public static void main(String[] args) {
try{
FileInputStream fis = new FileInputStream("d:/001.mp4");
FileOutputStream fos = new FileOutputStream("d:/001副本.mp4");
System.out.println("正在玩命拷贝中...");
/*方式一:int res = 0;
while ((res= fis.read())!=-1) {
fos.write(res);
}*/
/*方式二:int len = fis.available();
System.out.println("实际读取到的文件大小"+len);
byte[] bArr = new byte[len];
int res=fis.read(bArr);
fos.write(bArr);*/
byte[] bArr = new byte[1024*8];
int res = 0;
while ((res = fis.read(bArr))!=-1){
fos.write(bArr,0,res);
}
System.out.println("拷贝完成...");
fos.close();
fis.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
public class FileDemo01 {
public static void main(String[] args) {
File file = new File("d:/a.txt");
if (file.exists()){
String fileName = file.getName();
long len = file.length();
System.out.println("文件的名称是:"+fileName + ",文件的大小:" + len);
System.out.println(file.delete()?"删除成功" : "创建失败");
}else{
try{
System.out.println(file.createNewFile()?"删除成功" : "创建失败");
} catch (IOException e){
e.printStackTrace();
}
}
}
}
public class FileDemo02 {
public static void main(String[] args) {
File file = new File("d:/捣乱/猜猜我是谁/你猜我猜不猜/死鬼");
if (file.exists()){
System.out.println(file.delete()?"删除成功" : "创建失败");
}else {
System.out.println(file.mkdir()?"创建目录成功" : "创建目录失败");
}
}
}
public class FileInputStreamDemo01 {
public static void main(String[] args) {
try{
FileInputStream fis = new FileInputStream("d:/aa.txt");
int res = 0;
while ((res = fis.read())!=-1){
System.out.println((char)res);
}
fis.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
public class FileOutputStreamDemo01 {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("d:/aaa.txt",true);
fos.write(97);
fos.write('a');
fos.write("从前有座山,山上有座庙".getBytes());
System.out.println("写入成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public class ObjectInputStreamDemo01 {
public static void main(String[] args) {
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/cc.txt"));
Object o = ois.readObject();
System.out.println(o);
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public class ObjectOutputStreamDemo01 {
public static void main(String[] args) {
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/cc.txt"));
User user = new User("张家麟","123456","1383838438");
oos.writeObject(user);
System.out.println("写入成功");
oos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
public class PrintStreamDemo01 {
public static void main(String[] args) {
try{
PrintStream ps = new PrintStream("d:/heihei.txt");
ps.print("鲜衣怒马少年时,不负韶华行且知");
System.out.println("写入文件成功");
ps.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
public class User implements Serializable {
private String name;
private String password;
private String phonenumber;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", phonenumber='" + phonenumber + '\'' +
'}';
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public User(String name, String password, String phonenumber) {
this.name = name;
this.password = password;
this.phonenumber = phonenumber;
}
public User() {
}
}