Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi i was having issues completetly understanding this feature of my notepad. I want the user ot search for any .txt file they desire in their directory and be able to open it. Remember this is a notepad so the file must be readeable and writable. I created the simple Open but i get stuck in the fact that i keep getting red in br = new BufferedReader(new FileReader(open));, Only on the new FileReader(open)); part. How can i fix this? Any help is appreciated.
public void actionPerformed (ActionEvent event) {
if(event.getSource() == this.newFile){
this.textarea.setText("");
}else if(event.getSource() == this.openFile){
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
FileReader fr;
BufferedReader br;
if(option == JFileChooser.APPROVE_OPTION){
try{
br = new BufferedReader(new FileReader(open));
//while(){
//}
}catch(Exception ex){
System.out.println("");
}
}
}
}
JFileChooser is not a File, it can not be used as a parameter to FileReader, instead, you need to get the selected File from the chooser...
File choosenOne = open.getSelectedFile();
br = new BufferedReader(new FileReader(choosenOne ));
There are options available to you to make your life a little easier, like filters.
Check out How to use File Choosers for more details
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have some trouble reading a file. I have my input.txt in contents package, but the program still can't open the file.
String line = null;
try{
//loen faili
FileReader fileReader = new FileReader("contents/input.txt");
BufferedReader buffReader = new BufferedReader(fileReader);
while((line = buffReader.readLine()) != null){
System.out.println(line);
}
buffReader.close();
}catch(FileNotFoundException ex){
System.out.println("Error opening file");
}catch(IOException ex){
System.out.println("Error reading file");
}
Because you need to differnetly open files from your app packages than the one from disk, try:
InputStream is = getClass().getClassLoader().getResourceAsStream("contents/input.txt");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
the line of code I'm using is: InputStream is = game.class.getResourceAsStream("testing.txt"); but when I read the file it outputs null, however in the file there is the word "test", but when I run the program it still returns null, Help please :D
The code I am using is:
package Code;
import java.io.InputStream;
public class game {
public static void main(String[] args) {
InputStream is = game.class.getResourceAsStream("/testing.txt");
System.out.println(is);
}
I'm still unsure about your directory structure but this should work
public class game {
public static void main(String[] args) {
try{
InputStream is = game.class.getResourceAsStream("/testing.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
System.out.println(reader.readLine());
} catch( IOException e){
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Say I did a calculation:
1000 * 2000
How would I print the results of that calculation to a .txt file?
You can use the PrintWriter class. Something like:
PrintWriter w = new PrintWriter("filename.txt", "UTF-8");
w.println(calculation);
This will create a new file with the specified name, or it will overwrite one if it already exists.
I think your have trouble with variable types. Convert the Int to String. I haven't tried the code.
Integer.toString(1000 * 2000);
public static void saveAs() throws IOException {
System.out.println("Type In The New File Name"); //FILE NAME CHOICE
Scanner fileChoice = new Scanner(System.in); //this scans userinput
String newFile; //this is userinput
newFile = fileChoice.nextLine(); //whatever they type is stored in this var
File fileWriter = new File(newFile); //opens a new file
PrintWriter writeNew = new PrintWriter(new FileWriter(newFile, true)); //writes to it
writeNew.println("TEXT HERE BLAH") //writes the text to that file
writeNew.close(); //and dont forget to close
when the userinput will ask you for file name SPECIFY IT IS .TXT eg. type in hello.txt
problemsolved-
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want Some Code for eclipse to open file from internal storage, from Root directory and from system location.
can anyone help me?
thanks
The below lines may be what you wanted.
public static String readFromFile(String filePath){
String total="";
File file=new File(filePath);
try {
String encoding="GBK";
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到汉子编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
total+=lineTxt;
}
read.close();
}else if(!file.exists())
{
file.createNewFile();
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return total;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a short story with a couple of sentences and I have to transfer that to a string array.
Please refer to this article:
http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
specifically:
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}