Eclipse, android app[How TO] open TXT file [CODE]? [closed] - java

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;
}

Related

Trouble opening text file [closed]

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");

Java, automatic reading file from a directory [closed]

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.

How to get the page source from specific link in Java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to get the inputStream of particular website content or it's page source into String?
For instance, I want to download the whole html tag from particular website into string or xml. Is it possible?
Yes of course you just have to do something like
public static void main(String[] args) {
URL url;
try {
// get URL content
url = new URL("http://www.mkyong.com");
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
//save to this filename
String fileName = "/users/mkyong/test.html";
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
bw.write(inputLine);
}
bw.close();
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
CREDIT : mkyong
You may want to look at guava's CharStreams class.
CharStreams.toString(new InputStreamReader(..))
will save you from writing much boilerplate code.
Here is doc

How to put a text file into a string array? [closed]

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();
}

Creating an Open Function in Java using JFIleChooser [closed]

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

Categories

Resources