Unable to set character encoding in java.util.Scanner - java

I use Apache Tika to get encoding of file.
FileInputStream fis = new FileInputStream(my_file);
final AutoDetectReader detector = new AutoDetectReader(fis);
fis.close();
System.out.println("Encoding:" + detector.getCharset().toString());
I use Scanner to read values from file.
Scanner scanner = new Scanner(my_file, detector.getCharset().toString());
Map<String, String> values = new HashMap<>();
String line, key = null, value = null;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.contains(":")) {
if (key != null) {
values.put(key, value.trim());
key = null;
value = null;
}
int indexOfColon = line.indexOf(":");
key = line.substring(0, indexOfColon);
value = line.substring(indexOfColon + 1);
} else {
value += " " + line;
}
}
Scanner is unable to read text from files with encoding windows-1252, I get empty string.
UPDATE 2018.11.07.
I have same problem in case of BufferedReader.
Map<String, String> values = new HashMap<>();
String line, key = null, value = null;
FileInputStream is = new FileInputStream(my_file);
InputStreamReader isr = new InputStreamReader(is, getEncoding(my_file));
BufferedReader buffReader = new BufferedReader(isr);
while (buffReader.readLine() != null) {
line = buffReader.readLine();
if (line.contains(":")) {
if (key != null) {
values.put(key, value.trim());
key = null;
value = null;
}
int indexOfColon = line.indexOf(":");
key = line.substring(0, indexOfColon);
value = line.substring(indexOfColon + 1);
} else {
value += " " + line;
}
}

Instead of reading lines, I would try reading characters instead using the following approach:
ByteArrayOutputStream line = new ByteArrayOutputStream();
Scanner scanner = new Scanner(my_file);
while (scanner.hasNextInt()) {
int c = 0;
// read every line
while (c != newline) { // TODO: Check for a newline char
c = scanner.nextInt();
line.write((byte) c);
}
byte[] array = line.toByteArray();
String output = new String(array, "Windows-1252"); // This should do the trick
// We have a string here, do your logic
line.reset();
}
This approach is ugly, but uses new String which has the ability to specify a specific encoding. I did not test or run this code at all, but at least it will show you if any content is actually read properly.

Related

CSV date into arraylist_Java

CSV file
I am trying to save data separately by using ArrayList.
However, after saving one data into the array, it skips one line then saves the data. Please help me with this problem.
private void readCSVFile(String tickerCode, File file) {
System.out.println("Reading file " + tickerCode);
System.out.println(file.getPath());
try {
ArrayList<List<String>> line = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(file));
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAware(br);
int numLine = 0;
String[] values;
while ((values = csvReader.readNext()) != null) {
ArrayList<String> data = new ArrayList<>();
data.add(values[0]);
data.add(values[1]);
data.add(values[2]);
data.add(values[3]);
data.add(values[4]);
data.add(values[5]);
data.add(values[6]);
line.add(data);
if(numLine == 0){
System.out.print("StartDate = "+ values[0]);
}
if((csvReader.readNext() == null)){
System.out.println(" EndDate = " + values[0]);
}
++numLine;
}
debugger variable
As mentioned in a comment, you cannot use csvReader.readNext() inside the loop as well to check if the next element is null, because it will actually read the next line. So what you can do is keep the last processed row's date in a variable, and then print it as the EndDate after the while loop is finished.
ArrayList<List<String>> line = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(file));
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAware(br);
int numLine = 0;
String[] values;
String endDate = null;
while ((values = csvReader.readNext()) != null) {
ArrayList<String> data = new ArrayList<>();
data.add(values[0]);
data.add(values[1]);
data.add(values[2]);
data.add(values[3]);
data.add(values[4]);
data.add(values[5]);
data.add(values[6]);
line.add(data);
if(numLine == 0){
System.out.print("StartDate = "+ values[0]);
}
endDate = values[0];
++numLine;
}
if (endDate != null) { // Need to check null in case your CSV didn't have any rows
System.out.println(" EndDate = " + endDate);
}

Value from File is not matching with variable value

I am trying to match file value with variable value. But somehow it is not matching. I checked it is reading file and holding value in the variable but not matching. Not sure if I have to use contains function.
int rcdMatch = 0;
String st;
String extdeductamt = "1000";
BufferedReader Br = null;
File objFile = new File(strPlanFile + NewFileNmae);
Br = new BufferedReader(new FileReader(objFile));
List < String > list = new ArrayList < String > ();
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
while ((st = Br.readLine()) != null) {
arraylist = st;
String amt = arraylist.substring(0, arraylist.length() - 392);
list.add(amt);
Set < String > unique = new HashSet < String > (list);
for (String key: unique) {
rcdMatch = 0;
if (key.trim().toString().equals(stvar)) {
String adjAmt = arraylist.substring(34, arraylist.length() - 348);
System.out.println("Adjustment Amount 1 is: " + adjAmt);
if (extdeductamt.trim().toString().equals(adjAmt.trim().toString())) {
rcdMatch++;
}
if (!(rcdMatch == 0)) {
System.out.println("PASS Amount is displayed: " + adjAmt);
}
}
break;
}
}

Using FileWriter and keeping LineBreaks/WhiteSpace

Hello I'm currently using the Java FileWriter system with a HashTable to recreate a decoded message. I'm attempting to recreate a decoded message by looping through a file that contains all the encoded key value pairs (10111 = C) and writing to a text file
My Encoded KV Pairs
https://github.com/DijonLee/Project2/blob/master/freq.txt
prop.load(new FileReader(freqFile));
for (Map.Entry entry : prop.entrySet()) {
// map.put((String) entry.getKey(), (String) entry.getValue());
}
BufferedReader in = new BufferedReader(new FileReader(freqFile));
String line;
while ((line = in.readLine()) != null) {
// System.out.println(line); // ensure my line breaks are okay
if (line.contains("=")) {
String[] strings = line.split("=");
map.put(strings[0], (strings[1]));
}
}
Here is where I attempt to "Decode" my text based on the KV Pairs
FileReader inputStream = null;
inputStream = new FileReader("encoded.txt");
FileWriter outputStream = null;
outputStream = new FileWriter("ur_dec.txt");
int c;
String decoder = "";
String key = "";
while ((c = inputStream.read()) != -1) { // loop through file
char cToChar = (char) c; // get char
decoder += cToChar; // build string
if (map.containsValue(decoder)) {
for (Map.Entry entry : map.entrySet()) {
if (decoder.equals(entry.getValue())) {
key = (String) entry.getKey();
decoder = "";
outputStream.write(key); //
}
break; // breaking because its one to one map
}
}
}
Although my program works it appears to strip tabs and other white spaces that I'd like to keep while I encode and decode it and I'm not too sure why

Java Code check fields in duplicate, when value change start again

I want to write small java program to read data file first field and add seqcution number
Input file:
robert,190 vikign,...
robert,2401 windy,...
robert,1555 oakbrook,...
michell,2524 sprint,...
michell,1245 oakbrrok,...
xyz,2455 xyz drive,....
Output file should be:
robert,190 vikign,...,0
robert,2401 windy,...,1
robert,1555 oakbrook,...,2
michell,2524 sprint,...,0
michell,1245 oakbrrok,...,1
xyz,2455 xyz drive,....,0
Check first field when value change sequction number start back to 0 otherwise add sequction number by 1
here is my code:
public static void createseq(String str) {
try {
BufferedReader br = null;
BufferedWriter bfAllBWP = null;
File folderall = new File("Sort_Data_File_Out");
File[] BFFileall = folderall.listFiles();
for (File file : BFFileall) {
br = new BufferedReader(new FileReader(file));
String bwp = "FinalDataFileOut\\" + str;
bfAllBWP = new BufferedWriter(new FileWriter(bwp));
String line;
line = br.readLine();
String[] actionID = line.split("\\|");
String fullname = actionID[0].trim();
int seq = 0;
String fullnameb;
while ((line = br.readLine()) != null) {
actionID = line.split("\\|");
fullnameb = actionID[0].trim();
if(fullname.equals(fullnameb)) {
seq++;
}
else {
System.out.println(line + "======" + seq + "\n");
seq = 0;
fullname = fullnameb;
}
System.out.println("dshgfsdj "+line + "======" + seq + "\n");
}
}
}
catch(Exception letterproof) {
letterproof.printStackTrace();
}
}
The below code will fix the issue.I have updated the code if you face any pblm plz let me know :
Input :
robert,190 vikign,...
robert,2401 windy,...
robert,1555 oakbrook,...
michell,2524 sprint,...
michell,1245 oakbrrok,...
xyz,2455 xyz drive,....
Code :
public static void createseq() {
try {
File file = new File("d:\\words.txt"); //Hardcoded file for testing locally
BufferedReader br = new BufferedReader(new FileReader(file));
HashMap<String,Integer> counter = new HashMap<String, Integer>();
String line;
while((line = br.readLine())!= null)
{
String[] actionID = line.split(",");
String firstName = actionID[0];
if(counter.containsKey(firstName))
{
counter.put(firstName, counter.get(firstName) + 1);
}
else
{
counter.put(firstName,0);
}
System.out.println(line+" "+counter.get(firstName));
}
br.close();
} catch(Exception letterproof) {
letterproof.printStackTrace();
}
}
Ouput Come :
robert,190 vikign,... 0
robert,2401 windy,... 1
robert,1555 oakbrook,... 2
michell,2524 sprint,... 0
michell,1245 oakbrrok,... 1
xyz,2455 xyz drive,.... 0

Take Strings from Text file and assign each line to value (2 at a time and insert into LinkedHashMap)

What I'm trying to do is, load a Text file, then take the values from each line and assign them to a variable in my program. Every two lines, I will insert them into a LinkedHashMap (As a pair)
The problem with a buffered reader is, all I can seem to do is, read one line at a time.
Here is my current code:
public static void receiver(String firstArg) {// Receives
// Input
// File
String cipherText;
String key;
String inFile = new File(firstArg).getAbsolutePath();
Path file = new File(inFile).toPath();
// File in = new File(inFile);
try (InputStream in = Files.newInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(in))) {
String line = null;
while ((line = reader.readLine()) != null) {
// System.out.println(line);
String[] arrayLine = line.split("\n"); // here you are
// splitting
// with whitespace
cipherText = arrayLine[0];
// key = arrayLine[1];
System.out.println(arrayLine[0] + " " + arrayLine[1]);
cipherKeyPairs.put(arrayLine[0], arrayLine[1]);
}
} catch (IOException x) {
System.err.println(x);
}
The problem is, it can't find the arrayLine[1] (for obvious reasons). I need it to read two lines at a time without the array going out of bounds.
Any idea how to do this, so that I can store them into my LinkedHashMap, two lines at a time as separate values.
You can overcome this issue by inserting in the List every 2 lines reading.
A description for this code is that: "Bold is the true case"
Read the first line (count is 0)
If (secondLine is false) ==> Save the line to CipherText variable, make secondLine = true
Else If (secondLine is true) ==> Add to list (CipherText, line), make secondLine = false
Read the second line (count is 1)
If (secondLine is false) ==> Save the line to CipherText variable, make secondLine = true
Else If (secondLine is true) ==> Add to list (CipherText, line), make secondLine = false
String cipherText;
boolean secondLine = false;
String inFile = new File(firstArg).getAbsolutePath();
Path file = new File(inFile).toPath();
try {
InputStream in = Files.newInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (!secondLine) //first line reading
{
cipherText = line;
secondLine = true;
}
else if (secondLine) //second line reading
{
cipherKeyPairs.put(cipherText, line);
secondLine = false;
}
}
} catch (IOException x) {
System.err.println(x);
}
See if this works for you. I just edited your code. it might not be the best answer.
public static void receiver(String firstArg) {// Receives
// Input
// File
String cipherText;
String key;
String inFile = new File(firstArg).getAbsolutePath();
Path file = new File(inFile).toPath();
// File in = new File(inFile);
try (InputStream in = Files.newInputStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(in))) {
String line = null;
List<String> lines = new ArrayList();
while ((line = reader.readLine()) != null) {
lines.add(line);//trim line first though and check for empty string
}
for(int i=1;i<lines.size();i++){
cipherText = arrayLine[i];
// key = arrayLine[1];
System.out.println(arrayLine[i] + " " + arrayLine[i-1]);
cipherKeyPairs.put(arrayLine[i-1], arrayLine[i]);
}
} catch (IOException x) {
System.err.println(x);
}
}

Categories

Resources