java缓冲流写入缓存缓冲区刷新TXT文件乱码问题

记住登录一个月发表随想还能输入:200字该用户最新代码编程随想&by by by by by by [java]代码库import java.io.BufferedR
import java.io.F
import java.io.FileR
import java.io.RandomAccessF
import java.nio.ByteB
import java.nio.MappedByteB
import java.nio.channels.FileC
public class ReadBig {
public static String fff = "C:\\mq\\read\\from.xml";
public static void main1(String[] args) throws Exception {
final int BUFFER_SIZE = 0x300000;// 缓冲区大小为3M
File f = new File(fff);
* map(FileChannel.MapMode mode,long position, long size)
* mode - 根据是按只读、读取/写入或专用(写入时拷贝)来映射文件,分别为 FileChannel.MapMode 类中所定义的
* READ_ONLY、READ_WRITE 或 PRIVATE 之一
* position - 文件中的位置,映射区域从此位置开始;必须为非负数
* size - 要映射的区域大小;必须为非负数且不大于 Integer.MAX_VALUE
* 所以若想读取文件后半部分内容,如例子所写;若想读取文本后1/8内容,需要这样写map(FileChannel.MapMode.READ_ONLY,
* f.length()*7/8,f.length()/8)
* 想读取文件所有内容,需要这样写map(FileChannel.MapMode.READ_ONLY, 0,f.length())
MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")
.getChannel().map(FileChannel.MapMode.READ_ONLY,
f.length() / 2, f.length() / 2);
byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容
long start = System.currentTimeMillis();
for (int offset = 0; offset & inputBuffer.capacity(); offset += BUFFER_SIZE) {
if (inputBuffer.capacity() - offset &= BUFFER_SIZE) {
for (int i = 0; i & BUFFER_SIZE; i++)
dst[i] = inputBuffer.get(offset + i);
for (int i = 0; i & inputBuffer.capacity() - i++)
dst[i] = inputBuffer.get(offset + i);
int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
: inputBuffer.capacity() % BUFFER_SIZE;
System.out.println(new String(dst, 0, length));// new
// String(dst,0,length)这样可以取出缓存保存的字符串,可以对其进行操作
long end = System.currentTimeMillis();
System.out.println("读取文件文件一半内容花费:" + (end - start) + "毫秒");
}分享到:更多发表评论:评论须知:1、评论每次加2分,每天上限为30;2、请文明用语,共同创建干净的技术交流环境;3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。FileSend Java写的局域网文件传输代码,同过 流和缓冲流来 ,适合 初学者 Develop 238万源代码下载-
&文件名称: FileSend
& & & & &&]
&&所属分类:
&&开发工具: Java
&&文件大小: 13 KB
&&上传时间:
&&下载次数: 1
&&提 供 者:
&详细说明:Java写的局域网文件传输代码,同过文件流和缓冲流来传输,适合java初学者-LAN file transfer code written in Java
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&FileSend\.classpath&&........\.project&&........\last_position&&........\log.txt&&........\wtmpx&&........\src\Client\DMSClient.java&&........\...\bo\LogData.java&&........\...\..\LogRer.java&&........\bin\Client\DMSClient.class&&........\...\bo\LogData.class&&........\...\..\LogRer.class&&........\day04\Client.java&&........\.....\Server.java&&........\.....\udp\Client.java&&........\.....\...\Server.java&&........\src\Client&&........\...\bo&&........\bin\Client&&........\...\bo&&........\day04\udp&&........\src&&........\bin&&........\day04&&FileSend
&输入关键字,在本站238万海量源码库中尽情搜索:
&[] - java编写的局域网文件传输工具,具备聊天功能。适合初学者。&&&&彻底解决JAVA读取写入 TXT中文乱码问题 源码
彻底解决JAVA读取写入 TXT中文乱码问题 源码
彻底解决JAVA读取写入 TXT中文乱码问题 源码
若举报审核通过,可奖励20下载分
被举报人:
jerryding09
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
开发技术下载排行java上传文件到ftp-解决中文乱码的问题
java上传文件到ftp-解决中文乱码的问题
&&&&& java上传文件到ftp有两种实现方式,一种是使用sun公司提供的sun.net.ftp包里面的FtpClient,另一种是Apache组织提供的mons.net.ftp包里的FTPClient,现在我来分别说下两种实现方式。
sun的FtpClient:我们先来看如下代码:
public static boolean uploadFileBySun(StringBuffer fileContent,String server,String userName, String userPassword, String path, String fileName) {
&& &&& &FtpClient ftpClient = new FtpClient();
&& &&& &try {
&& &&& &&& &//打开ftp服务器
&& &&& &&& &ftpClient.openServer(server);
&& &&& &&& &//使用指定用户登录
&& &&& &&& &ftpClient.login(userName, userPassword);
&& &&& &&& &//转到指定路径
&& &&& &&& &ftpClient.cd(path);
&& &&& &&& &TelnetOutputStream os =
&& &&& &&& &//新建一个文件
//&& &&& &&& &os = ftpClient.put(new String(fileName.getBytes(&GBK&), &iso-8859-1&));
&& &&& &&& &os = ftpClient.put(fileName);
&& &&& &&& &OutputStreamWriter osw = new OutputStreamWriter(os);
&& &&& &&& &BufferedWriter bw = new BufferedWriter(osw);
&& &&& &&& &bw.write(fileContent.toString());
&& &&& &&& &bw.flush();
&& &&& &&& &bw.close();
&& &&& &} catch (Exception e) {
&& &&& &&& &System.out.println(e.getMessage());
&& &&& &&& &
&& &&& &} finally {
&& &&& &&& &try {
&& &&& &&& &&& &//关闭ftp连接
&& &&& &&& &&& &ftpClient.closeServer();
&& &&& &&& &} catch (Exception e) {
&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &}
正如上面的代码,上传文件分为六步,第一步,打开ftp服务器,第二步,使用指定用户名以及密码登陆,第三步,转到指定文件路径,第四步,创建一个文件,第五步,往文件里面写东西,并关闭文件,第六步,释放ftp连接。最后一步释放ftp连接很重要,一般ftp服务器连接数都是有限的,所以不管文件上传成功或是失败都必须释放连接。上面这个例子上传的文件是字符串文本,必须要提的是,如果上传的字符串文本较长(我项目中上传的文本大概在160kb上下),使用上面的方法可能会出现字符串丢失的情况,原因不明,可能跟缓存有关,所以如果文本较长,建议用户使用字节流。还有一个问题,如果要上传的文件名是中文的话,上传的文件名将是乱码,乱码问题我尝试许多转码也解决不了,于是不得不使用下面的方法了。
&& Apache的FTPClient:
public static boolean uploadFileByApacheByBinary(StringBuffer fileContent,String server,String userName, String userPassword, String path, String fileName) {
&& &&& &FTPClient ftpClient = new FTPClient();
&& &&& &try {
&& &&& &&& &InputStream is =
&& &&& &&& &is = new ByteArrayInputStream(fileContent.toString().getBytes());
&& &&& &&& &ftpClient.connect(server);
&& &&& &&& &ftpClient.login(userName, userPassword);
&& &&& &&& &ftpClient.changeWorkingDirectory(path);
&& &&& &&& &ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
&& &&& &&& &ftpClient.storeFile(new String(fileName.getBytes(&GBK&), &iso-8859-1&) , is);
&& &&& &&& &is.close();
&& &&& &} catch (Exception e) {
&& &&& &&& &e.printStackTrace();
&& &&& &&& &
&& &&& &} finally {
&& &&& &&& &if(ftpClient.isConnected()) {
&& &&& &&& &&& &try {
&& &&& &&& &&& &&& &ftpClient.disconnect();
&& &&& &&& &&& &} catch (Exception e) {
&& &&& &&& &&& &&& &e.printStackTrace();
&& &&& &&& &&& &}
&& &&& &&& &}
Apache上传文件的步骤跟sun的实现基本一致,只是方法名有些区别而已。在这里我将字符串文本转换成了ByteArrayInputStream字节缓冲流,这是个很有用的东西,常用来进行字符到流的转换。转换成字节上传就不会出现丢失文件内容的情况了。ftpClient.storeFile(new
String(fileName.getBytes(“GBK”), “iso-8859-1″) , is)这句代码将is输入流的东西上传到ftp服务器的fileName文件中,在这里我们对fileName文件名进行了转码,经测试中文没有乱码(ftp服务器使用的是window,其他平台未测试),而如果我们使用sun的ftp实现,即使文件名进行这样类似的转码,依然是乱码。
两种方法大家可以根据自己需要选择, 如果使用Apache的FTPClient的,我们需要在工程里面加入commons-net.jar,这个jar包我在这里提供给大家下载:
我的热门文章
即使是一小步也想与你分享BufferedWriter(Writer out)
创建一个使用默认大小输出缓冲区的缓冲字符输出流。
BufferedWriter(Writer out,
创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
可以指定缓冲区的大小,或者接受默认的大小。在大多数情况下,默认值就足够大了。
该类提供了 newLine() 方法,它使用平台自己的行分隔符概念,此概念由系统属性 line.separator 定义。并非所有平台都使用新行符 ('\n') 来终止各行。因此调用此方法来终止每个输出行要优于直接写入新行符。
通常 Writer 将其输出立即发送到底层字符或字节流。除非要求提示输出,否则建议用 BufferedWriter 包装所有其 write() 操作可能开销很高的 Writer(如 FileWriters 和 OutputStreamWriters)。例如,
intWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
将缓冲 PrintWriter 对文件的输出。如果没有缓冲,则每次调用 print() 方法会导致将字符转换为字节,然后立即写入到文件,而这是极其低效的。
public class BufferedReaderextends Reader
BufferedReader(Reader in)
创建一个使用默认大小输入缓冲区的缓冲字符输入流。
BufferedReader(Reader in,
创建一个使用指定大小输入缓冲区的缓冲字符输入流。
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
通常,Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求。因此,建议用 BufferedReader 包装所有其 read() 操作可能开销很高的 Reader(如 FileReader 和 InputStreamReader)。例如,
BufferedReader in
= new BufferedReader(new FileReader("foo.in"));
将缓冲指定文件的输入。如果没有缓冲,则每次调用 read() 或 readLine() 都会导致从文件中读取字节,并将其转换为字符后返回,而这是极其低效的。
通过用合适的 BufferedReader 替代每个 DataInputStream,可以对将 DataInputStream 用于文字输入的程序进行本地化
在创建缓冲区之前,必须先有流对象
package com.pb.io.demo2;
创建缓冲区之间必须,先创建流
使用缓冲区,要flush,刷新
创建缓冲区之间必须,先创建流
使用缓冲区,要flush,刷新
import .io.*;
class BufferedReaderAndWriterDemo1
public static void main(String[] args)
File source=new File("d:\\demo.txt");
File objFile=new File("d:\\a.txt");
useBuffered(source,objFile);
public static void useBuffered(File source,File objFile){
BufferedReader br=null;
BufferedWriter bw=null;
//声明缓冲区对象,并将字符流做为参数传入
br=new BufferedReader(new FileReader(source));
bw=new BufferedWriter(new FileWriter(objFile));
//接收读取的长度
String line=null;
//读取内容并放入buf中,判断是不是最后
while((line=br.readLine())!=null){
//写入,有多长写多长
bw.write(line,0,line.length());
bw.newLine();
System.out.println("读写完成!");
}catch(IOException e){
System.out.println(e.toString());
if(bw!=null)
bw.flush();
bw.close();
}catch(IOException e){
System.out.println(e.toString());
if(br!=null)
br.close();
}catch(IOException e){
System.out.println(e.toString());
二、MyBuffered
package com.pb.io.demo2;
import java.io.F
import java.io.FileNotFoundE
import java.io.FileR
import java.io.IOE
public class MyBuffered {
private FileR
public MyBuffered(FileReader fr) {
* 可以一次读一行的方法
public String myReadLine() throws IOException {
// 定义一个临时 容器,原BufferedReader封装的是字符数组
// 为了方便,定义一个StringBuilder
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch = fr.read()) != -1) {
if (ch == '\r') {
if (ch == '\n') {
return sb.toString();
// 读一个存一个
sb.append((char)ch);
if(sb.length()!=0){
return sb.toString();
return null;
public void myClose() throws IOException{
if (fr != null)
fr.close();
public static void main(String[] args) throws IOException {
File file=new File("d:\\demo.txt");
MyBuffered mb=new MyBuffered(new FileReader(file));
String line=null;
while((line=mb.myReadLine())!=null){
System.out.println(line);
mb.myClose();
三、装饰设计模式
3.1、装饰设计模式
装饰设计模式:
当想要对已经有的对象进行功能增强时,
可以定义类,将已经对象传入,基于已经有的功能,并提供加强功能。
那么自定义的该类就称为装饰类
package com.pb.io.demo3;
class Person{
public void eat(){
System.out.println("吃饭");
class SuperPerson{
public SuperPerson(Person p) {
public void superEat(){
System.out.println("开胃酒");
System.out.println("甜点");
System.out.println("来一根");
public class PersonTest {
public static void main(String[] args) {
Person p=new Person();
SuperPerson sp=new SuperPerson(p);
sp.superEat();
3.2、与继承之间的关系
装饰类通常会通过构造方法接收被被装饰的对象,并基于被装饰的对象的功能,提供更强的功能
装饰模式比继承灵活,避免了继承体系臃肿
而且降低了类于类之间的关系
装饰类因为增强已有对象,具备的功能和已经的是相同的,只不过提供了更强的功能。
所以装饰类和被装饰类通常是都属于一个体系中。
四、LineNumberReader
LineNumberReader(Reader in)
使用默认输入缓冲区的大小创建新的行编号 reader。
LineNumberReader(Reader in,
创建新的行编号
reader,将字符读入给定大小的缓冲区。
getLineNumber()
获得当前行号。
mark(int readAheadLimit)
标记该流中的当前位置。
读取单个字符。
read(char[] cbuf,
int off, int len)
将字符读入数组中的某一部分。
readLine()
读取文本行。
将该流重新设置为最新的标记。
setLineNumber(int lineNumber)
设置当前行号。
sk(long n)
跳过字符。
package com.pb.io.demo4;
import java.io.FileNotFoundE
import java.io.FileR
import java.io.IOE
import java.io.LineNumberR
public class LineNumberReaderDemo {
public static void main(String[] args) {
LineNumberReader lnr=null;
lnr=new LineNumberReader(new FileReader("d:\\demo.txt"));
//设置开始行叼
lnr.setLineNumber(100);
String line=null;
while((line=lnr.readLine())!=null){
//getLineNumber默认从1开始,设置后从设置的行号开始
System.out.println(lnr.getLineNumber()+":"+line);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
if(lnr!=null)
lnr.close();
} catch (IOException e) {
e.printStackTrace();
4.3、自定义
package com.pb.io.demo4;
import java.io.F
import java.io.FileR
import java.io.IOE
import java.io.R
public class MyLineNumberReader {
private int lineN
public MyLineNumberReader(Reader reader) {
this.reader =
public String myReadLine() throws IOException {
lineNumber++;
StringBuilder sb = new StringBuilder();
while ((ch = reader.read()) != -1) {
if (ch == '\r')
if (ch == '\n')
return sb.toString();
sb.append((char) ch);
if (sb.length() != 0)
return sb.toString();
return null;
public void myClose() throws IOException{
reader.close();
public Reader getReader() {
public void setReader(Reader reader) {
this.reader =
public int getLineNumber() {
return lineN
public void setLineNumber(int lineNumber) {
this.lineNumber = lineN
public static void main(String[] args) throws IOException {
File file=new File("d:\\demo.txt");
MyLineNumberReader mln=new MyLineNumberReader(new FileReader(file));
String line=null;
//mln.setLineNumber(100);
while((line=mln.myReadLine())!=null){
System.out.println(mln.getLineNumber()+":"+line);
mln.myClose();
五、字节流
5.1、字节流
OutputStream写、InputStream读
FileOutputStream、FileInputStream 字节流输出、输入流
OutputStream()
关闭此输出流并释放与此流有关的所有系统资源。
刷新此输出流并强制写出所有缓冲的输出字节。
write(byte[] b)
将 b.length 个字节从指定的 byte 数组写入此输出流。
write(byte[] b,
int off, int len)
将指定 byte 数组中从偏移量 off 开始的
len 个字节写入此输出流。
write(int b)
将指定的字节写入此输出流。
InputStream()
available()
返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。
关闭此输入流并释放与该流关联的所有系统资源。
mark(int readlimit)
在此输入流中标记当前的位置。
markSupported()
测试此输入流是否支持 mark 和 reset 方法。
从输入流中读取数据的下一个字节。
read(byte[] b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
read(byte[] b,
int off, int len)
将输入流中最多 len 个数据字节读入 byte
将此流重新定位到最后一次对此输入流调用 mark 方法时的位置。
skip(long n)
跳过和丢弃此输入流中数据的 n 个字节。
5.2、FileInputStream与FileOutputStream
package com.pb.io.demo5;
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
* 使用字节流复制文件
public class FileOutputAndFileInputStreamDemo {
public static void main(String[] args) {
File source=new File("d:\\1.jpg");
File objFile=new File("d:\\4.jpg");
InputAndOutput2(source,objFile);
public static void InputAndOutput(File source,File objFile){
//声明字节流输入,输出对象
FileInputStream fis=null;
FileOutputStream fos=null;
fis=new FileInputStream(source);
fos=new FileOutputStream(objFile);
//声明缓冲区
byte [] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
fos.write(buf, 0, len);
System.out.println("=======读写完成========");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
if (fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
* 不建议使用
public static void InputAndOutput2(File source,File objFile){
//声明字节流输入,输出对象
FileInputStream fis=null;
FileOutputStream fos=null;
fis=new FileInputStream(source);
fos=new FileOutputStream(objFile);
//声明大小刚刚缓冲区
byte [] buf=new byte[fis.available()]; //不建议使用available
fis.read(buf);
fos.write(buf,0,buf.length);
System.out.println("=======读写完成========");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
if (fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
六、字节流缓冲区
6.1、示例复制.mp3
package com.pb.io.demo5;
import java.io.BufferedInputS
import java.io.BufferedOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
public class BufferedStreamDemo {
public static void main(String[] args) {
File file1=new File("d:\\gm.mp3");
File file2=new File("d:\\mg.mp3");
copy1(file1,file2);
public static void copy1(File file1,File file2){
//声明字节流缓冲区对象
BufferedOutputStream bos=null;
BufferedInputStream bis=null;
bis=new BufferedInputStream(new FileInputStream(file1));
bos=new BufferedOutputStream(new FileOutputStream(file2));
//声明缓冲区大小
while((by=bis.read())!=-1){
bos.write(by);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
if(bos!=null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
if(bis!=null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
6.2、自定义
package com.pb.io.demo5;
import java.io.BufferedInputS
import java.io.BufferedOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import java.io.InputS
public class MyBufferedStream {
private InputS
private byte[] buf = new byte[1024];
private int pos = 0;
private int count = 0;
public MyBufferedStream(InputStream is) {
* 一次读一个字节,从缓冲区的(字节数组)获取
public int myRead() throws IOException {
// 通过in对象读取硬盘上的数据,并存在buf中
if (count == 0) {
count = is.read(buf);
if (count & 0) {
return -1;
byte b = buf[pos];
return b & 0xff;
} else if (count & 0) {
byte b = buf[pos];
return b & 0xff;
return -1;
public void myClose() throws IOException {
is.close();
public static void main(String[] args) {
File file1 = new File("d:\\gm.mp3");
File file2 = new File("d:\\kk.mp3");
copy1(file1, file2);
public static void copy1(File file1, File file2) {
// 声明字节流缓冲区对象
BufferedOutputStream bos = null;
MyBufferedStream mbs = null;
mbs = new MyBufferedStream(new FileInputStream(file1));
bos = new BufferedOutputStream(new FileOutputStream(file2));
// 声明缓冲区大小
int by = 0;
while ((by = mbs.myRead()) != -1) {
bos.write(by);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
mbs.myClose();
} catch (IOException e) {
e.printStackTrace();
7.1、字节流转换字符流
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。&
Writer out
= new BufferedWriter(new OutputStreamWriter(System.out));
接收键盘输入,使用转换流实现
package com.pb.io.demo6;
import java.io.BufferedR
import java.io.BufferedW
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import java.io.InputStreamR
import java.io.OutputStreamW
* 接收键盘输入,并将内容写入文件
public class Demo {
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter bw=null;
//声明输入流,使用转换流实现
br=new BufferedReader(new InputStreamReader(System.in));
//声明输出流,使用转换流实现
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\t.txt")));
//接收键盘录入
String str=null;
System.out.println("请输入要写入文件的内容,输入over结束!");
//输入over结束
while(!(str=br.readLine()).equals("over")){
bw.write(str);
bw.newLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
if(bw!=null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
if(br!=null)
br.close();
} catch (IOException e) {
e.printStackTrace();
八、流操作的规律
1、明确源和目的
  源:输入流。InputStream,Reader
  目的:输出流 .OutputStream,Writer
2、操作的数据是否为纯文本
  是:字符流。Reader、Writer
  不是:字节流。InputStream、OutputStream
3、当体系明确后,在明确要使用哪个具体对象。
  通过设备来进行区别。
  源设备:内存、硬盘、键盘
  目的设备:内存、硬盘、控制台
九、重定向IO
//设置输入流的
System.setIn(new FileInputStream("d:\\demo.txt"));
//设置的输出流的方向
System.setOut(new PrintStream("d:\\zz.txt"));
十、系统信息
10.1、示例
package com.pb.io.demo6;
import java.util.P
public class Demo1 {
public static void main(String[] args) {
Properties prop=System.getProperties();
prop.list(System.out); //可以设置为输出流对象new PrintWriter(文件)
-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Java\jre1.8.0_60\bin
java.vm.version=25.60-b23
java.vm.vendor= Corporation
java.vendor.url=http:///
path.separator=;
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
user.script=
user.country=CN
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=
java.vm.specification.name=Java Virtual Machine Specification
user.dir=F:\work\IODemo
java.runtime.version=1.8.0_60-b27
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Java\jre1.8.0_60\lib\endorsed
os.arch=amd64
java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\
line.separator=
java.vm.specification.vendor=Oracle Corporation
user.variant=
sun.jnu.encoding=GBK
java.library.path=C:\Java\jre1.8.0_60\C:\Windows\Su...
java.specification.name=Java Platform API Specification
java.class.version=52.0
piler=HotSpot 64-Bit Tiered Compilers
os.version=6.1
user.home=C:\Users\Administrator
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=UTF-8
java.specification.version=1.8
user.name=Administrator
java.class.path=F:\work\IODemo\bin
java.vm.specification.version=1.8
sun.arch.data.model=64
java.home=C:\Java\jre1.8.0_60
mand=com.pb.io.demo6.Demo1
java.specification.vendor=Oracle Corporation
user.language=zh
awt.toolkit=sun.awt.windows.WToolkit
=mixed mode
java.version=1.8.0_60
java.ext.dirs=C:\Java\jre1.8.0_60\lib\C:\Window...
sun.boot.class.path=C:\Java\jre1.8.0_60\lib\resources.jar...
java.vendor=Oracle Corporation
file.separator=\
java.vendor.url.bug=http:///bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=amd64
优质网站模板

我要回帖

更多关于 java写入文件中文乱码 的文章

 

随机推荐