Storing values to be retrieved by user - java

I'm working on a program that allows the user to input and view passwords that are encrypted when inputted and decrypted when accessed by the user. I figured that this could be done using a text document, however I'm unsure of how to get my code to interact with said document. Any ideas on how to do this/alternative ways that I can accomplish my task?

You could save your input in a text file with BufferedWriter and read the document with BufferedReader.
An example would be:
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(password)
out.close
BufferedReader in = new BufferedReader(new FileReader(file));
while((line = in.readLine()) != null){
System.out.println(line);
}
in.close

Related

Regarding word search error (Encoding error Java)

I have a list of french words where I am trying to search in my database. The words are "thé Mariage frères", "thé Lipton" etc.
While I am reading my file in java, it shows the words as "thé Lipton", "thé Mariage frères". It fails to get the correct words.
I don't know how to correct my errors.
Help me, please!!!
You file is in one encoding (maybe latin1/iso-8859-1) and you're reading your file in another encoding.
See if this port helps How to read a file in Java with specific character encoding?
Try this.
try (FileInputStream fis = new FileInputStream("input.txt");
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr)) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
}
Try creating Scanner object like this
Scanner s = new Scanner(new File("French_Tea_keywords/filter_keywords.txt"), "UTF8");

Reading text from file in GUI

I am stuck at reading text from a file into text area.I don't know why but my file reader never opens the file even if it exists.I am getting file name from a text field and using a button listener to trigger this event.So any help will be appreciated. I've given my code to below.
try{
BufferedReader br = new BufferedReader(new FileReader(tf1.getText()));
while((read = br.readLine())!=null){
store = store + read;
}
ta.setText(store);
fr.close();
br.close();
jf2.dispose();
}
catch(Exception exp){
JOptionPane.showMessageDialog(null,"File Not Found.");
}
Change your code to something like this:
br = new BufferedReader(new FileReader(new File(tf1.getText())));
It is important to note that you need to have a "File" that encapsulates your text to open the actual file. Otherwise, the JVM does not know in which part of the harddisk to search for.
Good luck.

Replicate "Save as HTML complete" in Java Action

How can I replicate the "Save as HTML, complete" command in a Java action?
I need to save a webpage, but it has local references such as "css/screen.css" or "images/logo.png".
If I manually save it with Firefox, for instance, I get a contents folders so that it all works.
Didn't find a way to do the same in Java, all I have is a code to download just the HTML source, but it still has local references.
Any tip?
URL url = new URL("http://www.google.it");
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter
(new FileWriter("data.html"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
I know I may parse the whole html source in another moment and replace all references, but doesn't sound as a good choice. So, is there a way to download the WHOLE content?

character encoding issue in converting csv data using fileInputStream class in java

Hi am having a csv file in my hand and am trying to read each line in csv file and update the content to a database table. Am doing this using java.
Following are the things i did for achieving this.
FileInputStream fileInputStream = FileUtils.openInputStream("filename.csv");
dataInputStream = new DataInputStream(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
String strLine;
while((strLine = bufferedReader.readLine()) != null){
System.out.println(strLine);
}
but its printing something like blocks on the console, not the actual data csv having
can anyone please help me to solve this issue.?
you should find out what encoding it is and then declare it while reading, xample for UTF-16:
new InputStreamReader(zipFile.getInputStream(entry), "UTF_16" )

why does this reader read off strange bits of data?

I'm trying to read a text file, i'm using fileImputStream, and reading all the lines into a single String then outputing it into the console (System.out)
When I try to read the humanSerf.txt, it gives me this in the consol:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\fs24 \cf0 symbol=HS\
strength=15\
agility=13\
constitution=7\
wisdom=9\
intelligence=5}
in the text file, it says this:
symbol=HS
strength=15
agility=13
constitution=7
wisdom=9
intelligence=5
How do I make the weird text disappear?
this is the code i'm using, please help
try{
// Open the file that is the first
// command line parameter
FileInputStream read = new FileInputStream("resources/monsters/human/humanSerf.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(read);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
How do I make the weird text disappear?
ps, this was done in mac textedditor
I think your text file is not plain text but rather a RTF file which supports formatting. When you view it you probably use a tool which supports RTF, such as TextEdit. If you view it with less or cat you should also see the RTF markup.

Categories