文件创建读取写入

文件创建

File file=new File("tmp1.txt");
		if(file.exists()==false){
			file.createNewFile();
			System.out.println("已创建新文件!");
		}

文件读取

<pre name="code" class="java">//字符流读出
FileInputStream in=new FileInputStream("tmp1.txt");
			FileOutputStream out=new FileOutputStream(new File("tmp2.txt"));
			InputStreamReader isr=new InputStreamReader(in);
			BufferedReader br=new BufferedReader(isr);
			char[]bs=new char[100];
			int c;
			String data=br.readLine();
			while(data!=null){
				System.out.println(data);
				data=br.readLine();
			}
//字节流读出
FileInputStream in=new FileInputStream("tmp1.txt");
FileOutputStream out=new FileOutputStream(new File("tmp2.txt"));
byte[]bs=new byte[100];
int c;
while((c=in.read(bs, 0, bs.length))!=-1){
<span style="white-space:pre">	</span>System.out.println(Arrays.toString(bs));
}

 文件写入 
 

FileInputStream in=new FileInputStream("tmp1.txt");
			FileOutputStream out=new FileOutputStream(new File("tmp2.txt"));
			InputStreamReader isr=new InputStreamReader(in);
			OutputStreamWriter osw=new OutputStreamWriter(out, "gbk");
			BufferedReader br=new BufferedReader(isr);
			BufferedWriter bw=new BufferedWriter(osw);
			String data=br.readLine();
			
			while(data!=null){
				System.out.println(data);
				bw.write(data);
				bw.newLine();
				data=br.readLine();
			}
			br.close();bw.close();







你可能感兴趣的:(java)