import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/**
*
* @author Administrator
*/
public class IOTest {
public static void main(String[] args){
try{
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream[] fos=new FileOutputStream[100];
for(int i=0;i<fos.length;i++){
fos[i]=new FileOutputStream("a"+i+".txt");
int read=fis.read();
while(read!=-1){
fos[i].write(read);
read=fis.read();
}
fos[i].close();
}
fis.close();
}
catch(IOException e){
e.printStackTrace();
}
}}
问题在于fis.read()移到-1后怎么移回去....
import java.io.FileOutputStream;
import java.io.IOException;/**
*
* @author Administrator
*/
public class IOTest {
public static void main(String[] args){
try{
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream[] fos=new FileOutputStream[100];
for(int i=0;i<fos.length;i++){
fos[i]=new FileOutputStream("a"+i+".txt");
int read=fis.read();
while(read!=-1){
fos[i].write(read);
read=fis.read();
}
fos[i].close();
}
fis.close();
}
catch(IOException e){
e.printStackTrace();
}
}}
问题在于fis.read()移到-1后怎么移回去....
public static void main(String[] args){
try{
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream[] fos=new FileOutputStream[100];
for(int i=0;i <fos.length;i++){
fos[i]=new FileOutputStream("a"+i+".txt");
int read=fis.read();
while(read!=-1){
fos[i].write(read);
read=fis.read();
}
fis.close();
fos[i].close();
fis = new FileInputStream("a"+i+".txt");
System.out.println(1);
}
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException{
File f = new File("C:/Users/hxbot/Desktop/a.txt");
if (f.exists()) {
Reader fis = null;
BufferedReader bfr = null;
Writer write = null;
BufferedWriter bfw = null;
String temp = "";
for(int i=0; i<100 ; i++){
fis = new FileReader(f);
bfr = new BufferedReader(fis);
write = new FileWriter(new File("C:/Users/hxbot/Desktop/a" + (i+1) + ".txt"));
bfw = new BufferedWriter(write);
while( (temp = bfr.readLine()) != null ){
bfw.write(temp);
bfw.newLine();
}
bfw.flush();
}
bfw.close();
write.close();
bfr.close();
fis.close();
} else {
System.out.println("源(src)文件不存在,请先创建。");
}
}
}
* 复制目录下单个文件
*
* @param oldPass
* @param newPath
* @return
*/
public Boolean copyFile(String oldPath, String newPath) {
try {
File file = new File(oldPath);
if (file.isDirectory()) {
return false;
} if (file.exists()) {
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(newPath);
BufferedInputStream reader = new BufferedInputStream(
inputStream);
int len = inputStream.available();
byte[] buffer = new byte[len]; reader.read(buffer, 0, len);
outputStream.write(buffer); inputStream.close();
outputStream.close(); reader.close(); }
} catch (Exception e) {
System.out.println("copy file failed!");
e.printStackTrace();
}
return true;
} /**
* 复制目录下所有文件和目录
*
* @param oldPath
* @param newPath
*/
@SuppressWarnings("static-access")
public Boolean copyAllFile(String oldPath, String newPath) { File file = new File(oldPath); if (!file.exists()) {
return false;
} if (file.isFile()) {
copyFile(oldPath, newPath);
return false;
} File[] fileList = file.listFiles();
File mkdir = new File(newPath); if (!mkdir.exists()) {
mkdir.mkdirs();
} for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
copyAllFile(oldPath + fileList[i].separator
+ fileList[i].getName(), newPath
+ fileList[i].separator + fileList[i].getName());
} else {
copyFile(oldPath + fileList[i].separator
+ fileList[i].getName(), newPath
+ fileList[i].separator + fileList[i].getName());
}
} return true; }自己写的,不敢保证效率