Results 1 to 3 of 3

Thread: File access with Java

  1. #1
    Join Date
    Dec 2008
    Posts
    161

    File access with Java

    The following are some programs that are used for accessing the files in Java.

    Write and Run a command file with PrintWriter :

    Code:
    Main.java 
      public class Main 
      (Public static void main (final java.lang.String [] args) 
      throws java.lang.Throwable 
      (Final java.io.File outFile = new java.io.File ("generated.bat"); 
      final java.io. PrintWriter out = 
      new java.io.PrintWriter (new java.io.FileOutputStream (outFile), true); 
      for (int i = 0; i <60; + + i) out. println 
      ("Wget http://www.example.com/resource" + i + ". Text"); 
      out. close (); 
      if (java.awt.Desktop.isDesktopSupported ()) 
      java.awt.Desktop.getDesktop (). open (outFile);))
    Reading lines with scanner (fragments) :

    Code:
    Fragment 
      wordStream = new java.io.FileInputStream ("C: / R/n/S/WINDOWS/Desktop/it16000.txt");
    
     wordScanner = new java.util.Scanner (wordStream);
    
     while (wordScanner. hasNextLine ())
    
     (Final java.lang.String Wordline = wordScanner. NextLine ();) 
    Fragment 
      final int number = new java.util.Scanner ("I have nine peanuts"). skip ("\ \ D +"). nextInt (); 
    Fragment 
      scanner. skip ("References:");
    
     scanner. useDelimiter ("[\ t \ r \ n] +");
    
     S.nextInt System.out.println (());
    
     S.nextInt System.out.println (());
    
     S.next System.out.println (());
    
     S.next System.out.println (()); 
    Fragment 
      if (scanner. hasNextInt ())
    
     (Final int i = scanner. NextInt ();) 
    Fragment 
      if (hasNext scanner. ("<[^>]+>"))
    
     (Final java.lang.String to = scanner. Next ("<[^>]+>");)

  2. #2
    Join Date
    Dec 2008
    Posts
    161

    Re: File access with Java

    The following text describes traditional classes, but which will be to work with simple text files (as shown above) does not necessarily need. Files by Java objects of class "java.io.File" illustrated in. Objects of this class can choose from a path, such as the path "C: / example.txt", are produced.
    Path
    final java.lang.String sourceFileName = "C: / example.txt";
    File
    final java.io.File sourceFile = new java.io.File (sourceFileName);

    Reading from streams :

    The class "java.io.FileInputStream" can read streams from a given file to deploy. When a file is not found, can be thrown such a stream of a controlled exception of type java.io.FileNotFoundException production in the case.
    Code:
    Current 
      (/ * Throws java.io.FileNotFoundException * / 
      final java.io.FileInputStream FileInputStream 
      = New java.io.FileInputStream (sourceFile);
    
       try 
      (/ * Use the FileInputStream * /)
    
       finally 
      (FileInputStream.close ();))
    By the close of objects from which something can be read, the resource used by these objects for the use which might be shared by other parts of the program. Therefore it is shown how such objects are closed as soon as possible and also the occurrence of exceptions again. If the reasoning for such an object fails, it could be closed later on dissolution of the object also automatically - but it is not sure if it comes before the end of this program and when is it appropriate to come. The direct reading from such a power should not be treated.

    Reading of texts :
    • java.io.InputStreamReader :
      If text is read from a byte stream, it is necessary to interpret the bytes as text. There are reader classes whose name ends with "reader" Ends. You need the name of an encoding, as this is needed to interpret the bytes as text and there are several different encodings in question, which can not always clearly drawn from the stream itself.
      Code:
      Reader 
        (/ * Throws java.io.UnsupportedEncodingException * / 
        final java.io.InputStreamReader input stream reader 
        = New java.io.InputStreamReader (FileInputStream, "UTF-8");
      
         try 
        (/ * Use the input stream reader * /)
      
         finally 
        (InputStreamReader.close ();))
      If the encoding is not specified, a putative coding is used. However, as this can lead to errors, it is recommended to specify the encoding time.
    • java.io.FileReader :
      The convenience class "java.io.FileReader" allows an object to an extension of "java.io.InputStreamReader" directly from a file path to create.
      Code:
      Reader 
        / * Throws java.io.FileNotFoundException * / 
        final java.io.InputStreamReader input stream reader 
        Java.io.FileReader = new ("C: / example.txt");
      This should be avoided, because here is missing the definition of a code, without which there can be errors.

  3. #3
    Join Date
    Dec 2008
    Posts
    161

    Re: File access with Java

    Buffers of texts :

    Reading a single character from a source is a relatively slow process. Faster would be to read too many characters at once. When characters are needed, a buffer is used, the internal read many characters at once, but then request each providing a single character. This will speed up the provision of a sign in general. The buffering object should be as far out are in the chain of transmission so that it can transmit as far as possible larger blocks instead of individual characters. Therefore, the buffering object is usually created last.
    Code:
    Buffer 
      (Final java.io.BufferedReader BufferedReader 
      = New java.io.BufferedReader (input stream reader);
    
       try 
      (/ * Use the BufferedReader * /)
    
       finally 
      (BufferedReader.close ();))

Similar Threads

  1. Replies: 22
    Last Post: 03-05-2012, 02:06 PM
  2. Replies: 3
    Last Post: 21-03-2012, 09:10 AM
  3. Replies: 3
    Last Post: 19-12-2010, 07:57 AM
  4. Replies: 3
    Last Post: 11-12-2010, 11:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,708,836.82617 seconds with 17 queries