62,620
社区成员
发帖
与我相关
我的任务
分享
while ((flag=in.read(temp)) != -1) {
System.out.println(new String(temp));
}
int tempByte;
while((tempByte=in.read())!=-1)
{
System.out.write(tempByte);
}
把这段改成String s;
while((s = in.readLine()) != null) {
System.out.println(s);
}
试试 readLine();一行一行的读BufferedReader bufr =
new BufferedReader(new InputStreamReader(file));
调readLine();import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("d:/1.txt");
OutputStream out = null;
InputStream in = null;
try {
file.createNewFile();
out = new FileOutputStream(file);
out.write("12345789\r\n一二三四五六七八九".getBytes());
in = new FileInputStream(file);
int tempByte;
byte[] b = new byte[1024];
while ((tempByte = in.read(b)) != -1) {
// System.out.write(tempByte);
System.out.write(b, 0, tempByte);
}
} catch (IOException e) {
e.printStackTrace();
} finally {//关闭流
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("d:/1.txt");
try {
file.createNewFile();
OutputStream out = new FileOutputStream(file);
out.write("12345789\r\n一二三四五六七八九".getBytes());
InputStream in = new FileInputStream(file);
int tempByte;
byte[] b = new byte[1024];
while ((tempByte = in.read(b)) != -1) {
// System.out.write(tempByte);
System.out.write(b, 0, tempByte);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IO {
public static void main(String []args) {
//String fileName = "D:" + File.separator;
String fileName = "D:" + File.separator + "HelloWorld.txt";
File file = new File(fileName);
try {
//file.createNewFile();
//System.out.println("创建文件成功!");
//OutputStream out = new FileOutputStream(file,true);
//String str = "霹雳神兵";
// String str = "\r\n Rollen";
// byte [] b = str.getBytes();
// System.out.println(b);
// for(int i = 0; i < b.length; i++ ) {
// out.write(b[i]);
// }
// out.close();
// System.out.println("写入成功。");
InputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int len = in.read(b);
in.close();
System.out.println("长度" + len);
System.out.println("读取成功!" + new String(b,0,len));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
//print(file);
}
public static void print(File file) {
if(file != null) {
if(file.isDirectory()) {
File [] f = file.listFiles();
if(f != null) {
for(int i = 0; i< f.length; i++) {
print(f[i]);
}
}
} else {
System.out.println(file);
}
}
}
}