• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting content from a file  RSS feed

 
Simon Cookridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to open a file from within a GUI and get the content (a Description String & simple DNA bases e.g. ATG) from this file to display within an input area of the GUI. I want to do this by passing the filepath to a makeSequence method. I have the code below which enables me to apparently open the file using the JFileChooser and display the file path in the command window showing this is done. However I am unable to get the content from the file either in the command window or in my Input areas in the GUI. Any pointers would be much appreciated.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Cookridge wrote:I am trying to open a file from within a GUI and get the content (a Description String & simple DNA bases e.g. ATG) from this file to display within an input area of the GUI. I want to do this by passing the filepath to a makeSequence method. I have the code below which enables me to apparently open the file using the JFileChooser and display the file path in the command window showing this is done. However I am unable to get the content from the file either in the command window or in my Input areas in the GUI. Any pointers would be much appreciated.


Your requirements are a little vague. JFileChooser returns a file path, it does not open the file. You'd need code to open the file, read it into a single long String, and add that to a JTextArea widget. That's assuming I interpreted your requirements correctly.
 
Simon Cookridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Thank you. I also have code to open the file:



I’m not sure how to go about reading the file into a single long string, as the code I have (commented out in bold below) hasn’t worked. I’m not sure what should go in the brackets as I have errors with what I have.



Alternatively, I’m not sure if something like a Buffered reader should be used to complete the implementation?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon, I have to tell you, you're going about this business all backwards. Instead of removing the code which you have questions about and posting the rest, you should really show us the code you have questions about.

So far you're halfway there. But you've commented out some code which calls a method named "readAllBytes", and presumably that's the method which "has errors". So why not show us the code of that method? It would also help if you described the errors related to it -- read our FAQ entry TellTheDetails (<-- click on that link) for more on how to ask good questions here.
 
Simon Cookridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O.k. thanks for the advice

This is my method:


And this is my error log:
TranslationTool.java:118: error: illegal escape character
Path myTextFile_path = Paths.get("C:\myJava\myTextFile.txt);
^
TranslationTool.java:118: error: illegal escape character
Path myTextFile_path = Paths.get("C:\myJava\myTextFile.txt);
^
TranslationTool.java:118: error: unclosed string literal
Path myTextFile_path = Paths.get("C:\myJava\myTextFile.txt);
^
TranslationTool.java:118: error: ';' expected
Path myTextFile_path = Paths.get("C:\myJava\myTextFile.txt);
^
TranslationTool.java:123: error: 'catch' without 'try'
catch (IOException e){
^

Advice would be appreciated, thanks
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot write \m. If you want a backslash you have to escape it. Write \\m.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The catch without try error probably means you are short of a } before try. You probably have an additional } somewhere.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… or you have forgotten a quote mark at the end of the String literal in line 2.
 
Simon Cookridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That makes sense. I have made changes:



However, I still get various 'cannot find symbol' errors, as below:

TranslationTool.java:117: error: cannot find symbol
public static byte[] readAllBytes(Path path) throws IOException{
^
symbol: class Path
location: class TranslationTool

TranslationTool.java:105: error: cannot find symbol
String content = new String(readAllBytes(get("myTextFile.txt")))
;
^
symbol: method get(String)
location: class TranslationTool

TranslationTool.java:118: error: cannot find symbol
Path myTextFile_path = Paths.get("C:\\myJava\\myTextFile.txt");
^
symbol: class Path
location: class TranslationTool

TranslationTool.java:118: error: cannot find symbol
Path myTextFile_path = Paths.get("C:\\myJava\\myTextFile.txt");
^
symbol: variable Paths
location: class TranslationTool

TranslationTool.java:120: error: cannot find symbol
byte[] myTextFileArray = files.readAllBytes(myTextFile_p
ath);
^
symbol: variable files
location: class TranslationTool


 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Cookridge wrote:Thanks! That makes sense. I have made changes:



However, I still get various 'cannot find symbol' errors, as below:

The symbol is missing because this method won't compile. You are missing a return;
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the "ATG" character set? Why are you using it?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you have a "path" parameter that you're passing in but never using.
 
Simon Cookridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the content of the file to return but I'm not sure what to call this. I get 6 errors with return myTextFile; (as below)



The ATG character set is the content in my file.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Cookridge wrote:I want the content of the file to return but I'm not sure what to call this. I get 6 errors with return myTextFile; (as below)



The ATG character set is the content in my file.


You don't have a variable called "myTextFile". Also you have to match the type of the variable you are returning to the return type in your method declaration, which in your case is byte[].

What character set is "ATG"? Most .txt files are UTF-8. What does ATG do that UTF-8 does not? Can you post a link to documentation on ATG? Google was not very helpful.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, what language are you storing in your .txt file?
 
Simon Cookridge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ATG" is just content which I have created in the file to represent DNA bases. By the sounds of it I have gone off track with the code above. All I want to do is open a file with simple content (a Description of a DNA sequence and DNA bases e.g. ATGCAT.. etc.) and display this in the input area of a GUI. I have code to choose & open the file and I can display which file I am selecting in the command window. However, I cannot actually get the content of the file to display in my input area in the GUI. I think I need to change chooser.getSelectedFile().getPath()); in the code below to do this.
When I get the selected file from the JFileChooser I want to call the getPath() method on the file and pass this file path to the makeSequence method (below). This is the bit I'm struggling with. The makeSequence method should return a new DNASequence object. I will be able to use this to set the description and content strings into my GUI.



 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, "ATG" is what you are calling a series of (English) characters that represent a DNA sequence. There is no such thing as a characterset (from a Java point of view) as "ATG", therefore all you need is


Did you write the class "DNASequence"?

Seems like you want to pass the selected File from JFileChooser to getemakeSequence() as a parameter instead of using "File myFile".

In getmakeSequence() you attempt to return "makeSequence" but there is no such variable.

It seems like you're making random changes without understanding what you are doing. At any point in time you should at least have code that compiles and it's clear from what you've shown us that this is not the case. How long is your complete .java code file? If it's not too big then post the whole thing instead of showing us pieces, that way we could see, for example, what you mean by "input area".

You must be getting compile errors. You shouldn't go any further without correcting those errors. If you need help with the errors you'll have to post the complete error message(s).

It may help to stop coding and write down on paper the steps you are trying to implement:
1. create gui
2. open JFileChooser when "Open" is clicked on the menu
3. pass the chosen File to a method to create a new DNA sequence
4. the method must open and read the entire file
5. create a new sequence from the data
6. display new sequence in the gui.

Something along those lines.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!