النتائج 1 إلى 3 من 3

الموضوع: How can I read from, and write to, files in Java?

  1. #1
    elfr3on el3ashk الصورة الرمزية A7med Baraka
    تاريخ التسجيل
    Jun 2008
    الدولة
    Egypt - Cairo
    المشاركات
    4,695
    معدل تقييم المستوى
    10

    Thumbs up How can I read from, and write to, files in Java?


    End Google Ads 201810 - BS.net 01 -->

    How can I read from, and write to, files in Java?

    Stand-alone applications written in Java can access the local file-system, but applets executing under the control of a browser cannot. With this in mind, lets take a look at two simple applications that write a line of text to a file, and read it back




    كود:
    import java.io.*;
    
    public class MyFirstFileWritingApp
    {
        // Main method
        public static void main (String args[])
        {
            // Stream to write file
            FileOutputStream fout;        
    
            try
            {
                // Open an output stream
                fout = new FileOutputStream ("myfile.txt");
    
                // Print a line of text
                new PrintStream(fout).println ("hello world!");
    
                // Close our output stream
                fout.close();        
            }
            // Catches any error conditions
            catch (IOException e)
            {
                System.err.println ("Unable to write to file");
                System.exit(-1);
            }
        }    
    }

    This simple application creates a FileOutputStream, and writes a line of text to the file. You'll notice that the file writing code is enclosed within a try { .... } catch block. If you're unfamiliar with exception handling in Java, this requires a little explanation.
    Certain methods in Java have the potential to throw an error condition, known as an exception. We have to trap these error conditions, so we 'catch' any exceptions that may be thrown.
    The task of reading from a file is also just as easy. We create a FileInputStream object, read the text, and display it to the user.


    كود:
    import java.io.*; public class MyFirstFileReadingApp { // Main method public static void main (String args[]) { // Stream to read file FileInputStream fin; try { // Open an input stream fin = new FileInputStream ("myfile.txt"); // Read a line of text System.out.println( new DataInputStream(fin).readLine() ); // Close our input stream fin.close(); } // Catches any error conditions catch (IOException e) { System.err.println ("Unable to read from file"); System.exit(-1); } }
    }


  • #2
    تقنى جديد
    تاريخ التسجيل
    Nov 2008
    المشاركات
    4
    معدل تقييم المستوى
    0

    افتراضي رد: How can I read from, and write to, files in Java?

    مشكووووووووووووووووووووووور أخي

  • #3
    تقنى جديد
    تاريخ التسجيل
    Apr 2009
    المشاركات
    1
    معدل تقييم المستوى
    0

    Wink رد: How can I read from, and write to, files in Java?

    Write In File In JAVA


    كود PHP:
    package IOstream;

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;

    public class 
    Writer {
    public static 
    void main(String[] args) {
    File f=new File("c:/MyFirstFile.txt");
    FileWriter fw;
    try {
    fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("Java Developer");
    bw.newLine();
    bw.close();
    System.out.println("Written");
    } catch (
    IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }





  • معلومات الموضوع

    الأعضاء الذين يشاهدون هذا الموضوع

    الذين يشاهدون الموضوع الآن: 1 (0 من الأعضاء و 1 زائر)

    المواضيع المتشابهه

    1. VTC Design Patterns For Java Programmers
      بواسطة wa7edmenelnass في المنتدى Java - جافا
      مشاركات: 7
      آخر مشاركة: 12-11-2010, 12:39 AM
    2. كتاب جافا Java How to program
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 14
      آخر مشاركة: 06-20-2009, 02:45 AM
    3. switch Statement in Java
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 11-02-2008, 03:19 AM
    4. try/catch Statement in java
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 10-31-2008, 12:28 AM

    الكلمات الدلالية لهذا الموضوع

    مواقع النشر (المفضلة)

    ضوابط المشاركة

    • لا تستطيع إضافة مواضيع جديدة
    • لا تستطيع الرد على المواضيع
    • لا تستطيع إرفاق ملفات
    • لا تستطيع تعديل مشاركاتك
    •  
    "وَقُل رَّبِّ زِدْنِي عِلْمًا"
    أعلانات نصية أستضافة , ريسيلر - Best Hosting | BarakaSoft Web Solutions

    BarakaSoft PageRank RSS RSS 2.0 XML MAP HTML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 C/C++ | Java | C# | Network | Database | OS | Linux | Windows | Hacker & Security | Photoshop | Flash | Web Development | Free Programs | Mobile App | Free Java Course | Latest Technical News | Internet Programs | Antiviurse Programs | Graphics Programs | Network Programs | Portable Programs | vb Forums Development | Forums Development | CMS(Joomla-nuke-wordpress-mkportal...) | Photo | Anime |