java中用流写文件时为何总是第一个字母没有被写入
importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.Bu...
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Exe1 {
public static void main(String[] args) {
int len;
/*try {
BufferedReader br=new BufferedReader(new FileReader("C:\\wdm1\\java\\workspace\\chapter11\\task2.txt"));
BufferedWriter bw=new BufferedWriter(new PrintWriter("chapter12.txt"));
while((len=br.read())!=-1){
String s=br.readLine();
//bw.write(s);
bw.write(s, 0, s.length());
}
br.close();
bw.flush();
bw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("chapter12.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("write.txt"));
byte b[]=new byte[200];
while((len=bis.read())!=-1){
bis.read(b);
bos.write(b, 0, len);
}
bis.close();
bos.flush();
bos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Exe1 {
public static void main(String[] args) {
int len;
/*try {
BufferedReader br=new BufferedReader(new FileReader("C:\\wdm1\\java\\workspace\\chapter11\\task2.txt"));
BufferedWriter bw=new BufferedWriter(new PrintWriter("chapter12.txt"));
while((len=br.read())!=-1){
String s=br.readLine();
//bw.write(s);
bw.write(s, 0, s.length());
}
br.close();
bw.flush();
bw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
try {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("chapter12.txt"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("write.txt"));
byte b[]=new byte[200];
while((len=bis.read())!=-1){
bis.read(b);
bos.write(b, 0, len);
}
bis.close();
bos.flush();
bos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
3个回答
展开全部
while((len=bis.read())!=-1){
read(); //读取单个字节,你每次进入循环判断都会执行。所以你应该不是第一个字母消失,应该是没200个字母就消失一个。
read(); //读取单个字节,你每次进入循环判断都会执行。所以你应该不是第一个字母消失,应该是没200个字母就消失一个。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
bos.write(b, 0, len);
这句写错了
第3个参数为要第2个参数后的多少位
给你一点我写的代码自己理解下
/**
* 从original 写入到target a为不要最后多少位 b为不要开始多少位 c为要a后的多少位
*
* @param original
* @param target
* @param a
* @param b
* @param c
* @throws IOException
*/
public void byteCopySpecified(String original, String target, int a, int b,
int c, boolean whether) throws IOException {
BufferedInputStream bufis = new BufferedInputStream(
new FileInputStream(original));
BufferedOutputStream bufos = new BufferedOutputStream(
new FileOutputStream(target, whether));
byte[] buf = new byte[bufis.available()];
bufis.read(buf, a, buf.length - a);
if (buf.length - a - b < c) {
c = buf.length - a - b;
}
bufos.write(buf, a + b, c);
bufos.close();
bufis.close();
}
这句写错了
第3个参数为要第2个参数后的多少位
给你一点我写的代码自己理解下
/**
* 从original 写入到target a为不要最后多少位 b为不要开始多少位 c为要a后的多少位
*
* @param original
* @param target
* @param a
* @param b
* @param c
* @throws IOException
*/
public void byteCopySpecified(String original, String target, int a, int b,
int c, boolean whether) throws IOException {
BufferedInputStream bufis = new BufferedInputStream(
new FileInputStream(original));
BufferedOutputStream bufos = new BufferedOutputStream(
new FileOutputStream(target, whether));
byte[] buf = new byte[bufis.available()];
bufis.read(buf, a, buf.length - a);
if (buf.length - a - b < c) {
c = buf.length - a - b;
}
bufos.write(buf, a + b, c);
bufos.close();
bufis.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
BufferedWriter bw=new BufferedWriter(new PrintWriter("chapter12.txt"));
while((len=br.read())!=-1){
String s=br.readLine();
//bw.write(s);
bw.write(s, 0, s.length());
-----------------------------修改-----------------------------------------------
BufferedWriter bw=new BufferedWrite(new FileWrite("D:/chapter12.txt"))
String ss;
while(ss.readLine()!=null){
bw.write(ss);
bw.newLine();
}
while((len=br.read())!=-1){
String s=br.readLine();
//bw.write(s);
bw.write(s, 0, s.length());
-----------------------------修改-----------------------------------------------
BufferedWriter bw=new BufferedWrite(new FileWrite("D:/chapter12.txt"))
String ss;
while(ss.readLine()!=null){
bw.write(ss);
bw.newLine();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询