successfully copied. But I found that when viewed in every 10,000 always lose a small part .
I tried to use the 9000 segment found will lose part or inadvertently , I do not understand why this is.
what is the code:
package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class CopyTXT {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\xia\\Desktop\\ss.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int i = 0;
int t = 0;
String str = null;
File target = null;
BufferedWriter bw = null;
while ( (str = br.readLine()) != null) {
if (i % 10000 == 0) {
System.out.println(i);
target = new File("C:\\Users\\xia\\Desktop\\"+t+".txt");
bw = new BufferedWriter(new FileWriter(target));
t ++;
}
i ++;
if (str.indexOf(i+":") == -1)
System.out.println("error"+i);
bw.write(str);
bw.write("\t\r\n");
}
br.close();
bw.close();
}
}
want to know who can explain
------ Solution ----------------------------- ---------------
a file is too large , more than ten trillion, I switched it to go into a lot of small copies. I think it should only be initialized in there , right?
ah , then suggested initialization, then bw into a list , and finally concentrated close processing .
e .................
some do not understand. BufferedWriter bw anyway just an object , defined outside the loop .
There is initialized numerous times even still had that BufferedWriter object ah , that needs to be closed list.size () times ? ? ? ? ? ? ?
bw just cited , the key depends on how many times you are new , this is the real content to be closed .
If you do not understand , give you an example set forth under the referenced object itself and the relationship it: you have a TV ( Object entity ) , you want to change the channel, then engage in a remote control ( reference). With this remote control we realize the various operations of the TV.
This is the way you like , change the remote control , and gave each room house to buy a TV ( all the same brand ) . Bw then you use your remote control, the TV home of one open , and finally just walked into the parlor , and then press the close button , so the feeling of all the TVs are off . Ah they are off it?
just an analogy Kazakhstan , LZ Thinking , or in each new place , direct printing under bw, see the printed object memory address is not not the same.
------ Solution ---------------------------------------- ----
reason for that issue? ?
io, io especially the write is not a quick response to the request , because it contains the memory of the data processing and disk io two parts.
disk io performance of which depends entirely on the operating system and hardware , and are familiar with the hardware people know that disk io rate is far less than the memory read and write speeds , copying large files tend to be less than negligible +1% cpu time memory io +99% disk io.
If direct call close () method may result in all the data into memory , data memory, but not all cases are handled hard disk , so that the entire size of the untreated portion was abandoned.
And when the flush method will refresh the memory , will wait for all the data in memory to disk io .
------ For reference only -------------------------------------- -
br.close ();
bw.close ();
could not read the file , you first shut down , try to replace the two lines of code
bw.close ();
br.close ();
purely conjecture, there is no experimental data to provide possible
------ For reference only ---------------------- -----------------
amount . . . not that look like.
because I was in a for loop inside do not generate real-time or real time off, and I die inside the loop operation is not affected by the outside world, on and off only after it is completed execution ,
I tried , nor
------ For reference only -------------------------- -------------
recommend your bw on the outside loop initialization , otherwise you new a lot, but only closed once.
In addition , bw.write ("\ t \ r \ n"); wrap want to achieve it? You can use bw.newLine (); implementation.
------ For reference only -------------------------------------- -
a file is too large , more than ten trillion, I switched it to go into a lot of small copies. I think it should only be initialized in there , right?
------ For reference only -------------------------------------- -
a file is too large , more than ten trillion, I switched it to go into a lot of small copies. I think it should only be initialized in there , right?
ah , then suggested initialization, then bw into a list , and finally concentrated close processing .
------ For reference only -------------------------------------- -
in close first flush ( before ) ( ) is a good habit.
------ For reference only -------------------------------------- -
a file is too large , more than ten trillion, I switched it to go into a lot of small copies. I think it should only be initialized in there , right?
ah , then suggested initialization, then bw into a list , and finally concentrated close processing .
e .................
some do not understand. BufferedWriter bw anyway just an object , defined outside the loop .
There is initialized numerous times even still had that BufferedWriter object ah , that needs to be closed list.size () times ? ? ? ? ? ? ?
------ For reference only -------------------------------------- -
reason for that issue? ?
------ For reference only -------------------------------------- -
problem has been solved , I , 6th Floor, opinions are very valuable .
modification of the Department :
bw.write(str);
bw.write("\t\r\n");
bw.flush();
are refreshed every written. . . While getting closer from the truth , but I still want to know the results
------ For reference only ----------------------- ----------------
if (i % 10000 == 0) {
System.out.println(i);
target = new File("C:\\Users\\xia\\Desktop\\"+t+".txt");
if (bw!=null){
bw.close()
}
bw = new BufferedWriter(new FileWriter(target));
t ++;
}
This should also be the
------ For reference only ----------------------------- ----------
a file is too large , more than ten trillion, I switched it to go into a lot of small copies. I think it should only be initialized in there , right?
ah , then suggested initialization, then bw into a list , and finally concentrated close processing .
e .................
some do not understand. BufferedWriter bw anyway just an object , defined outside the loop .
There is initialized numerous times even still had that BufferedWriter object ah , that needs to be closed list.size () times ? ? ? ? ? ? ?
bw just cited , the key depends on how many times you are new , this is the real content to be closed .
If you do not understand , give you an example set forth under the referenced object itself and the relationship it: you have a TV ( Object entity ) , you want to change the channel, then engage in a remote control ( reference). With this remote control we realize the various operations of the TV.
This is the way you like , change the remote control , and gave each room house to buy a TV ( all the same brand ) . Bw then you use your remote control, the TV home of one open , and finally just walked into the parlor , and then press the close button , so the feeling of all the TVs are off . Ah they are off it?
just an analogy Kazakhstan , LZ Thinking , or in each new place , direct printing under bw, see the printed object memory address is not not the same.
Brother Takami , I also thought of this problem. They certainly will not be the same memory address , after all, every new instance are not the same . And that really want to close the case, can only shut off before the new
if (bw != null)
bw.close();
target = new File("C:\\Users\\xia\\Desktop\\"+t+".txt");
bw = new BufferedWriter(new FileWriter(target));
// System.out.println(bw);
I flush the line removed, found files are flawlessly written into, it seems that this must be related with the stream is not closed .
probably the reason I think it should be delayed questions:
IO stream is written , when not enough time to write complete ( no refresh ) gave it new a new one , and that it did not stop when he stopped , and thus the emergence of this situation ,
I think I said before reading the flush, so ,
thank cough !
------ For reference only -------------------------------------- -
a file is too large , more than ten trillion, I switched it to go into a lot of small copies. I think it should only be initialized in there , right?
ah , then suggested initialization, then bw into a list , and finally concentrated close processing .
e .................
some do not understand. BufferedWriter bw anyway just an object , defined outside the loop .
There is initialized numerous times even still had that BufferedWriter object ah , that needs to be closed list.size () times ? ? ? ? ? ? ?
bw just cited , the key depends on how many times you are new , this is the real content to be closed .
If you do not understand , give you an example set forth under the referenced object itself and the relationship it: you have a TV ( Object entity ) , you want to change the channel, then engage in a remote control ( reference). With this remote control we realize the various operations of the TV.
This is the way you like , change the remote control , and gave each room house to buy a TV ( all the same brand ) . Bw then you use your remote control, the TV home of one open , and finally just walked into the parlor , and then press the close button , so the feeling of all the TVs are off . Ah they are off it?
just an analogy Kazakhstan , LZ Thinking , or in each new place , direct printing under bw, see the printed object memory address is not not the same.
Brother Takami , I also thought of this problem. They certainly will not be the same memory address , after all, every new instance are not the same . And that really want to close the case, can only shut off before the new
if (bw != null)
bw.close();
target = new File("C:\\Users\\xia\\Desktop\\"+t+".txt");
bw = new BufferedWriter(new FileWriter(target));
// System.out.println(bw);
I flush the line removed and found no errors in written documents also went in, it seems that this must be related with the stream is not closed .
probably the reason I think it should be delayed questions:
IO stream is written , when not enough time to write complete ( no refresh ) gave it new a new one , and that it did not stop when he stopped , and thus the emergence of this situation ,
I think I said before reading the flush, so ,
thank cough !
brother, if you can close the stream properly , flush what are the clouds .
没有评论:
发表评论