1Write a text file using FileWrite
2
3
4 PublicStaticvoidUsefilewriter (String fileName)throwsIOException {
5File File =NewFile (FileName);
6FileWriter FileWriter =NewFileWriter (file);
7
8Filewriter.write ("It is a Test");
9
TenFilewriter.close ();
One}
A
-
-
theWrite a text file using Bufferedwrite
-
-
- PublicStaticvoidUsebufferedwriter (String fileName)throwsioexception{
+File File =NewFile (FileName);
-BufferedWriter BufferedWriter =NewBufferedWriter (NewFileWriter (FileName));
+
ABufferedwriter.write ("Hello Bufferedwrite");
at
-Bufferedwriter.flush ();
-Bufferedwriter.close ();
-}
-
-
in
-Using files to write a file, the simplest
to
+
- PublicStaticvoidUseJdk8 (String fileName)throwsIOException {
theFiles.write (Paths.get (fileName), "Hello Usejdk8". GetBytes (), standardopenoption.create);
*}
$
Panax Notoginseng
-Private FileOutputStream Write file
the
+
A PublicStaticvoidUsefileoutputstream (String fileName)throwsioexception{
theFile File =NewFile (FileName);
+
-FileOutputStream FileOutputStream =NewFileOutputStream (file);
$Fileoutputstream.write ("Hello FileOutputStream". getBytes ());
$
-Fileoutputstream.flush ();
-
theFileoutputstream.close ();
-}
Wuyi
the
-
WuUse Bufferedfileoutputstream to write files, the fastest, the data cache in the JVM, easy to lose data
-
About
$ PublicStaticvoidUsebufferedfileoutputstream (String fileName) {
-File File =NewFile (FileName);
-
-
ABufferedoutputstream Bufferedoutputstream =NULL;
+Try{
theBufferedoutputstream =NewBufferedoutputstream (NewFileOutputStream (file));
-
$Bufferedoutputstream.write ("Hello Bufferedfileoutputstream". getBytes ());
the
theBufferedoutputstream.flush ();
the}Catch(IOException e) {
the
-
in}finally{
theif(bufferedoutputstream!=NULL) {
the Try{
AboutBufferedoutputstream.close ();
the}Catch(IOException E1) {
the//TODO do something
the}
+}
-}
the
Bayi}
the
the
-
-Use Randomaccessfile to write files with the slowest speed, direct brush disk
the
the
the PublicStaticvoidUserandomaccessfile (String fileName) {
theRandomaccessfile Randomaccessfile =NULL;
-
the
theTry{
theRandomaccessfile =NewRandomaccessfile (FileName, "RW");
94Randomaccessfile.seek (15);//writes from the 15th byte position, and the 15th character of the original file is partially overwritten
theRandomaccessfile.write ("Userandomaccessfile". GetBytes ());
the}Catch(IOException e) {
theE.printstacktrace ();
98}finally{
About if(randomaccessfile!=NULL) {
-Try{
101Randomaccessfile.close ();
102}Catch(IOException e) {
103E.printstacktrace ();
104}
the}
106
107}
108}
109
the
111
theWrite files using FileChannel
113
the
the PublicStaticvoidUsefilechannel (String fileName) {
the
117FileChannel FileChannel =NULL;
118Try{
119FileChannel channel =NewFileOutputStream (FileName). Getchannel ();
-Channel.write (Bytebuffer.wrap ("Usefilechannel". GetBytes ()));
121}Catch(IOException e) {
122E.printstacktrace ();
123}finally{
124if(filechannel!=NULL) {
theTry{
126Filechannel.close ();
127}Catch(IOException e) {
-E.printstacktrace ();
129}
the}
131}
the}
133
134
135
136Write files using Mappedbytebuffer, fast, OS-level memory mapping
137
138
139 PublicStatic voidUsemappedbytebuffer (String fileName) {
$
141Randomaccessfile Randomaccessfile =NULL;
142
143Try{
144Randomaccessfile =NewRandomaccessfile (FileName, "RW");
145FileChannel FileChannel = Randomaccessfile.getchannel ();
146
147
148String content = "Usemappedbytebuffer";
149
MaxMappedbytebuffer Mappedbytebuffer = Filechannel.map (FileChannel.MapMode.READ_WRITE, 0, Content.getbytes (). length);
151
theMappedbytebuffer.put (Content.getbytes ());//Mappedbytebuffer size cannot be less than the number of content bytes
153
154
155}Catch(IOException e) {
156E.printstacktrace ();
157}finally{
158if(randomaccessfile!=NULL) {
159 Try{
theRandomaccessfile.close ();
161}Catch(IOException e) {
162E.printstacktrace ();
163}
164}
165}
166}
Java Write file