parameter: The associated object that is bound to the IO operation public class Servercompletionhandler implements Completionhandlerpublic class Client {private asynchronoussocketchannel ASC; Public Client () throws Exception {ASC = Asynchronoussocketchannel.open (); } public void Connect () throws Interruptedexception, executionexception{//Get () block Asc.connect (new I Netsocketaddress ("127.0.0.1", 8765)). get (); } public void write (Strin
class provides the ability to read data in advance, which is the function of buffering. You can take a look at its Read method: public synchronized int read () throws IOException { if (pos >= count) { fill (); if (POS >= count) return-1; } return Getbufifopen () [pos++] 0xFF; }As you can see, when Pos>=count, it is time to buffer some data beforehand, then call Fill () to fill the buffe
The previous File class only operates on the File itself. If you operate on the File content, you can use the RandomAccessFile class, you can randomly read data at a specified position in a file.
Because all the content in the file is stored in byte and has a fixed storage location.
Constructor:
Public RandomAccessFile (File file, String mode) throws FileNotFoundException
When instantiating this class, you must pass the File class. Tell the program which file should be operated, followed by a m
();
}
}
Copy CodeOperation Result:650) this.width=650; "id=" aimg_294 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132250nrc1di5kpr2zi16d.png "class=" Zoom "width=" 349 "alt=" 132250nrc1di5kpr2zi16d.png "/>The main disadvantage of this method of reading is to build a byte array as large as the file size, when the file is small, and when the file is large, memory may not be able to frame such a large array of bytes. Therefore, this method is only suitable
=NewFileOutputStream (NewFile ("Test.txt")); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } bufferedoutputstream Bos=NewBufferedoutputstream (FOS); String C= "Java is an object-oriented programming language that can compose cross-platform applications, and is the Java programming language and Java platform (ie, javase)
here is the default character set.Writing is also similar to the process as shown in:Through the OutputStreamWriter class, the character-to-byte encoding process is completed by the Streamencoder encoding process.Example: using the transform stream inputstreamreader and OutputStreamWriterThe conversion flow is required when converting between a byte stream and a character stream, or when you want to encode bytes of data PackageOrg.example.io;ImportJava.io.File;ImportJava.io.FileInputStream;Impo
{Test t = new Test (); T.I + = 5; FileOutputStream fos = new FileOutputStream ("c:\\users\\gaofangquan\\desktop\\ new folder \ \ New text document (6). txt"); O Bjectoutputstream Oos = new ObjectOutputStream (FOS); Oos.writeobject (t); Oos.flush (); Oos.close (); FileInputStream fis = new FileInputStream ("c:\\users\\gaofangquan\\desktop\\ new folder \ \ New text document (6). txt"); O Bjectinputstream ois = new ObjectInputStream (FIS); Test t_read = (test) ois.readobject (); System.out.println
There are too many APIs related to I/O operations in Java, and for historical reasons, some APIs have been deprecated, sometimes confusing for some beginners, today, I took the time to sort out frequently used stream operations and share them out. If there are any mistakes, please correct them because most of the methods have been commented out, so the test code in main will not write comments.
For theoretical information, refer to NLP.
2008.07.03 was
= Fileinputstream.read (BUF))! =-1) {System. out. Print (NewString (BUF,0, length)); } } Catch(IOException e) {/**//Processing the code ... First you want to block the subsequent code execution, and you need to notify the caller that something went wrong ... throw new * runtimeexception (e); *//Pass the IOException to the RuntimeException wrapper layer, and then throw it, the
When the socket receives data, it often throws Java. io. the eofexception is abnormal and there are no clear causes or prompts. Many people are asking this question on the Internet, but no practical solution is found. After research, the problem is solved. The exception stack information is as follows: Java. Io. eofexc
Import Java. Io. file;2 Import Java. Io. ioexception; 3 4 Public Class Test1 { 5 Public Static Void Main (string [] ARGs ){ 6 File F = New File ("D:" + file. Separator + "test.txt "); // To increase portability, we recommend that you use file. separator.
within ObjectInputStream and then called ReadObject () to get a reference that points to an object that is being converted upward, so it must be transformed downward to set them directly. The following is the sample code for serialization and deserialization:@Test Public voidtestwriteserialization () {Try {FileOutputStream FileOutputStream =NewFileOutputStream ("H:\\serialize.txt"); ObjectOutputStream ObjectOutputStream=NewObjectOutputStream (FileOutputStream); Objectoutputstream.writeobject ("
,//stream can manipulate the file object Pro.store (FW, "after Change");}
Iv. Practice
Get the number of uses of an application, more than 3 times, give information about the number of times the usage has been registered, and do not run the program again
Analysis:
This requirement requires a counter, each time the program starts counter into memory, number of times +1, exit program, counter close, store to file.
Because the confidence to be clear, there should be a name and numbe
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.