نمایش نتایج 1 تا 5 از 5

نام تاپیک: دسترسی تصادفی به فایل

  1. #1

    دسترسی تصادفی به فایل

    سلام
    من با دسترسی تصادفی به فایل مشکل دارم
    من از کلاس randomAccessFile استفاده کردم ولی وقتی می نویسم :
    RandomAccessFile raf = ....
    raf.seek(0);
    هیچ اتفاقی نمی افته .برنامه رو خط به خط که اجرا می کنم می بینم از این خط رد می شه و کاری نمی کنه و پوینتر هم آخر فایل می مونه

    ممنون می شم اگه کسی بگه اشکالش چیه؟!

  2. #2
    کاربر دائمی آواتار Bahram0110
    تاریخ عضویت
    آبان 1384
    محل زندگی
    شیراز | یاسوج | اهواز
    پست
    1,059
    سلام
    من java زیاد بلد نیستم
    این مثال رو می زارم شاید به دردتون بخوره

    public FileOutputStream(String name, boolean append)
    throws IOException
    in Java 1.0, however, you must use the java.io.RandomAccessFile class that lets you read and write bytes from arbitrary locations in a file. this class implements DataInput and DataOutput so you have all the methods of DataInputStream and DataOutputStream available to you.
    To create a new random access file pass the name of the file and the mode to the constructor. The mode is either "r" (read-only) or "rw" (read and write). The length() method returns a long that tells you how many bytes there are in a file and the seek(long p) method lets you position the file pointer at a particular point in the file. Thus to start writing at the end of a RandomAccessFile raf, you first raf.seek(raf.length()). The following example demonstrates by appending the string "Kilroy was here!" to every file specified on the command line.
    (Java FAQ:found on the web at:http://sunsite.unc.edu/javafaq/javafaq.html)
    // By:
    //**************************************
    //

    import java.io.*;


    class AppendToAFile {


    public static void main (String args[]) {


    for (int i = 0; i < args.length; i++) {
    //First open the file you want to append
    // to


    try {
    RandomAccessFile raf = new RandomAccessFile(args[i], "rw");
    // Position yourself at the end of the f
    // ile
    raf.seek(raf.length());
    // Write the String into the file. Note
    // that you must
    // explicitly handle line breaks.
    raf.writeBytes("\nKilroy was here!\n");
    }


    catch (IOException e) {
    System.out.println("Error opening file: " + e);
    }

    }
    }
    }



    منبع : http://www.pscode.com/vb/scripts/Sho...d=258&lngWId=2

  3. #3
    مرسی از مثالتون ولی من خودم هم این مثالو دیده بودم
    مشکل من وقتیه که میخوام برم اول فایل
    با آخر فایل مشکلی ندارم

    اگه sample code ی دارین که به اول فایل رفته باشه ممنون میشم به من هم بدین

    مرسی

  4. #4
    سلام
    ببخشید من اون مشکلم حل شد ولی حالا یه مشکل دیگه دارم
    من می خوام به یه جای خاص از برنامه seek کنم .
    وقتی طول اون رشته ی موجود رو می گیرم 5 هست.و فایل پوینتر من 7 هست .ولی وقتی من میگم seek کنه و به اون یه آرگومان پاس می کنم که هست:getFile Pointer-str.length-2 به جایی که باید نمی ره و دو تا کاراکتر جلوتره .نمی دونم چرا .اگه کسی حوصله ی run کردن برنامه رو داره می تونم Source رو بذارم ؟

    ممنون

  5. #5
    من به شما کتاب j2se 5 herbert schildt رو توصیه میکنم که البته ترجمه فارسی اون رو انتشارات کانون نشر علوم چاپ کرده

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •