ورود

View Full Version : مبتدی: خواندن از فایل



Miladsob
سه شنبه 12 مهر 1390, 00:33 صبح
من میخوام وردیهای برنامه‌ام رو از یک فایل بگیرم، با سرچی که کردم مثل اینکه باید از exeption handleingاستفاده کنم! مثل کد زیر:


package MyProject

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {

public static void main(String[] args) {

File file = new File("C:\\MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {

// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}



ولی این ارور های زیر رو نشون میده:


warning: [options] bootstrap class path not set in conjunction with -source 1.6
NetBeansProjects\proj1\src\proj1\Proj1.java:8: error: illegal escape character
File file = new File("C:\RHDSetup.txt");

\NetBeansProjects\proj1\nbproject\build-impl.xml:604: The following error occurred while executing this line:

\NetBeansProjects\proj1\nbproject\build-impl.xml:246: Compile failed; see the compiler error output for details.

spiderman200700
سه شنبه 12 مهر 1390, 16:08 عصر
کد هاتون رو اجرا کردم.کاملا درست کار میکنن.
یه پرژه جدید توی NetBeans درست کنید و کدهاتون رو توی اون پروژه بنویسید.
اینجوری به احتمال زیاد درست میشه.

Miladsob
سه شنبه 12 مهر 1390, 17:34 عصر
دستتون درد نکنه!
ولی فهمیدم مشکل از کجا بود، من توی مسیر فایلی که میدادم، یه دونه backSlash میذاشتم، که درستش اینه که دوتا باشه!