package com.dasenlin.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedInputStreamTest {
public static void main(String[] args) {
String src="conf/newt.properties";
String dest="c:/test/my.txt";
File srcfile = new File(src);
File destfile = new File(dest);
if(!destfile.getParentFile().exists()){
destfile.getParentFile().mkdir();
}
BufferedInputStream in=null;
BufferedOutputStream out=null;
try {
in=new BufferedInputStream(new FileInputStream(srcfile));
out=new BufferedOutputStream(new FileOutputStream(destfile));
byte [] bytes = new byte[4096];
int len=-1;
while((len=in.read(bytes, 0, bytes.length))!=-1){
out.write(bytes,0,len);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in!=null)in.close();
if(out!=null)out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.dasenlin.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class CharsetDemoTest {
public static void main(String[] args) {
File file = new File("src/utf8.txt");
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
char [] arr = new char[50];
int len=-1;
while((len=reader.read(arr))!=-1){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]);
}
}
reader.close();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file2 = new File("src/utf8.txt");
try {
FileInputStream in = new FileInputStream(file2);
byte [] by = new byte[1024];
int len=-1;
while((len=in.read(by,0,by.length))!=-1){
System.out.print(new String(by,"utf-8"));
}
in.close();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.dasenlin.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DataOutputStreamTest {
final static String FILEPATH="src/utf8.txt";
public static void main(String[] args) {
Person [] per = {new Person("aa", 12, true),new Person("bb", 22, false)};
try {
//writeToFile(per);
readToFile();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readToFile() throws IOException{
File file =new File(FILEPATH);
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
List<Person> pl = new ArrayList<>();
int len=-1;
while(true){
Person pe = new Person();
pe.name=in.readUTF();
pe.age=in.readInt();
pe.sex=in.readBoolean();
pl.add(pe);
System.out.println(pl);
}
}
private static void writeToFile(Person[]person) throws IOException{
File file =new File(FILEPATH);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
for(Person p:person){
out.writeUTF(p.name);
out.writeInt(p.age);
out.writeBoolean(p.sex);
out.flush();
}
out.close();
}
}
class Person{
String name;
Integer age;
Boolean sex;
public Person() {
}
public Person(String name, Integer age, Boolean sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
package com.dasenlin.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class LineNumberReaderTest {
public static void main(String[] args) {
File file = new File("src/com/dasenlin/io/Test.java");
try {
LineNumberReader lineReader = new LineNumberReader(new FileReader(file));
String s;
while((s=lineReader.readLine())!=null){
System.out.println(lineReader.getLineNumber()+":\t\t"+s);
}
lineReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.dasenlin.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class ReaderTest {
public static void main(String[] args) {
try {
File file = new File("src/com/dasenlin/io/Test2.java");
Reader reader = new FileReader(file);
char [] arr = new char[200];
while(true){
int len = reader.read(arr);
if(len==-1){
break;
}
for(int i=0;i<len;i++){
System.out.print(arr[i]);
}
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
File file = new File("src/com/dasenlin/io/Test2.java");
Reader reader = new FileReader(file);
char [] arr = new char[200];
int len=-1;
while((len=reader.read(arr, 0, arr.length))!=-1){
for(int i=0;i<len;i++){
System.out.print(arr[i]);
}
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.dasenlin.io;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
public class WriterTest {
public static void main(String[] args) {
try {
File tempDir = new File(".");
String tmpPath = tempDir.getCanonicalPath();
Scanner scan = new Scanner(System.in);
System.out.println("please input filename");
String filename =scan.nextLine();
System.out.println(tmpPath);
File file = new File(tmpPath+File.separator+filename);
if(!file.exists()&&!file.isFile()){
file.createNewFile();
}
Writer writer = new FileWriter(file);
while(true){
System.out.println("please input content");
String content = scan.nextLine();
if(content.length()==0)break;
writer.write(content);
writer.write("\r\n");
writer.flush();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}