PDA

View Full Version : دسترسی تصادفی به فایل



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

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

Bahram0110
پنج شنبه 23 فروردین 1386, 10:41 صبح
سلام
من 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/ShowCode.asp?txtCodeId=258&lngWId=2

parvin1987
پنج شنبه 23 فروردین 1386, 11:49 صبح
مرسی از مثالتون ولی من خودم هم این مثالو دیده بودم
مشکل من وقتیه که میخوام برم اول فایل
با آخر فایل مشکلی ندارم

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

مرسی

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

ممنون

programmingir
شنبه 08 اردیبهشت 1386, 01:25 صبح
من به شما کتاب j2se 5 herbert schildt رو توصیه میکنم که البته ترجمه فارسی اون رو انتشارات کانون نشر علوم چاپ کرده