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");
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want that , by pasting the URL of a file (the file can be an image, an Xhtml, or a Css) into a form of a JSP, this can be downloaded form internet and saved locally. Please can you help me ?
you can use this to open URL in the browser and save into the file location.
<%
String site= contain the string(URL);
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
File file = new File("/Users/asdf.xml");
FileWriter fr = null;
BufferedWriter br = null;
URL url = new URL(site);
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
fr = new FileWriter(file);
br = new BufferedWriter(fr);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
br.write(line);
br.newLine();
}
reader.close();
br.close();
%>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to know if it's possible to do this:
i've some .txt file in a directory in my filesystem
i would like to write a java code that does this:
Automatically read all the files in the directory
Give me a output
Exists some library? or it's just a code problem?
It's possible?
Thanks
Reads & prints the content
public static void main(String[] args) {
List<String> li=new TestClass().textFiles("your Directory");
for(String s:li){
try(BufferedReader br = new BufferedReader(new FileReader(s))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
System.out.println(everything);
} catch (IOException e) {
e.printStackTrace();
}
}
}
For getting all Text files in the Directory
List<String> textFiles(String directory) {
List<String> textFiles = new ArrayList<String>();
File dir = new File(directory);
for (File file : dir.listFiles()) {
if (file.getName().endsWith((".txt"))) {
textFiles.add(file.getPath());
}
}
return textFiles;
}
Of course it's possible. You need to look at File, Reader classes. A useful method is File#listFiles. Happy coding.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am experiencing very strange behavior with BufferedReader. I want to read an entire file however it only reads every other line.
E.g the file below
1 //ignore the left most space - shouldn't exist
2
3
4
5
6
Will output
2
4
6
Here is some of my code...
fileRead = new BufferedReader(new InputStreamReader( new FileInputStream(file)));
public void scan(){
if (fileRead != null){
try{
while ((fileRead.readLine()) != null){
String line = fileRead.readLine();
String abcLine = line;
System.out.println(line);
}
}catch(IOException e) {
System.out.println("Line can not be read");
}
}else{ System.out.println("Can not Read - File Not Found"); }
}
My best bet is the bug lies within the while statement. Is this the correct way to ensure
you read the file until you reach EOF "end of file" ?
Any insight is truly appreciated
Thank you!
You're reading two lines each time through the loop. Your current code is:
while ((fileRead.readLine()) != null){ // reads a line, ignores it
String line = fileRead.readLine(); // reads another line, stores in 'line'
... // do stuff with 'line'
}
Every call to readLine() reads a line. You probably want something more like:
String line;
while ((line = fileRead.readLine()) != null) {
... // do stuff with 'line'
}
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
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