Replacing a specific string from a text file in Java - java

I've got a text file like this: Image
The first column represents the user ID and the last column represents balance. I want to iterate through the elements to find a specific user ID and then update their balance. I've managed to match the user ID and access the balance using the Scanner but I'm not sure how to update the balance in the text file with a new value. This is the code I've written so far:
File file = new File("UserInfo.txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
String info = sc.nextLine();
String data[] = info.split(" ");
//userId is a function argument
if(String.valueOf(userId).equals(data[0])){
//I want to update the balance in the text file with a new value here
}else{
continue;
}
}
sc.close();

it seems that you want to create a CSV file. My advice is to use ";" as a separator for the split() command, and if the file is not too big, read the whole file, put it into an arrayList of Strings and then manipulate It, rewriting the whole file at the end from the arrayList. Otherwise you have to use RandomAccess class

Replacing string in file is not so simple. You have to read whole file and rewrite part that you want to leave unchanged. Only when condition of comparing userID and data[0] is met, you would use replace() function to change user balance.
Your file is basically CSV file with space as line separator, as already mentioned store file contents in array or BufferedReader, change appropriate line and then export back to CSV.

Related

How to read a CSV file, remove a column, and replace the file

I have a CSV file in my local folder. I would like to read it, remove a column, and replace the file in the same folder.
Actual sample data:
ID,EMAIL,FIRSTNAME,LASTNAME
99999,TestEmail#fakeemail.com,TEST_FNAME,TEST_LNAME
33333,TestEmail#fakeemail.com,TEST_FNAME,TEST_LNAME
Expected data from the sample data:
ID,EMAIL,FIRSTNAME
99999,TestEmail#fakeemail.com,TEST_FNAME
33333,TestEmail#fakeemail.com,TEST_FNAME
In this case, I would like to remove the column LASTNAME. Can it be done effectively in Java?
As in your example, you want to remove last column.
So easiest way would be to substring from beginning to last index of separator ,.
line.substring(0,line.lastIndexOf(","))
Full example:
try(PrintWriter pw = new PrintWriter(Files.newBufferedWriter(
Paths.get("path_to_output_file")))) {
Files.lines(Paths.get("path_to_input_file")).map(line -> line.substring(0,line.lastIndexOf(","))).forEach(pw::println);
}
If you need to remove other column, you may you split line.split(",") and than concatenate skipping column you want.

How do I get a specific line from a text file into an array list?

I want to get specific lines of data from a text file using arraylist.
(more context: student details are stored in Student.txt.
if i have to update a specific student, i want to get that student's line from text file into an arraylist in order to edit it)
Text file looks like this (ID, Name, Degreelevel, Email, Conatctno : courses);
A30, sarah, sarah#gmail.com, +64732 ; Computer Science, Cyber Security, Digital Media
A45,zaha, zaha#gmail.com, +3683: Software Engineering
My code prints all the data in the text file into the arraylist.
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
ArrayList<String> lines = new ArrayList<>();
String line = reader.readLine();
while (line != null)
{
lines.add(line);
line = reader.readLine();
}
reader.close();
System.out.println(lines);
}
current output:[ID, Name, Degreelevel, Email, Conatctno : courses, A30, sarah, sarah#gmail.com, +64732 ; Computer Science, Cyber Security, Digital Media, A45,zaha, zaha#gmail.com, +3683: Software Engineering]
So how can i search a specific student ID and input only that line into arraylist. Any hint helps. Thank you.
You have to go over each line until you find your matching id. This is one of the reasons why we typically like to store this type of information in databases rather than text files.
Simply add an if-statement that checks you got the right line before you add it to the arraylist.
A very simple way of doing this would be:
String myId = "A30";
while (line != null){
if(line.startsWith(myId){
lines.add(line);
break;
}
line = reader.readLine();
}
Normally when parsing text files one would split up each line using some data delimiter, e.g. line.split(","), rather than using startsWith.
However, the data in your example is badly structured, using both colon, semi-colon and comma as delimiters. One common way of dealing with the problem that a string contains a delimiter is to encapsulate all string in quotation marks, and treat any special character found within quotation marks as a regular character.
A semi-formalized structured format for data in text file is csv. Most languages (including Java) has libraries for parsing csv files.

parsing the csv file to import to android app

i am working with android app to import the csv file to populate my DB.
I am using OpenCSV library to do it. I wish to use CSV reader to read from the inputstream.
I am using import feature from the gmail, so i use android:scheme=content
it makes csv file come as InputStream.
Inside I have 3 columns. Some rows have , character.
So when I try to separate columns using , I get error.
I wish help to know how to overcome this issue of comma inside the columns.
You could get your csv with another separator character? if yes, the you can call yor csv reader with this separator, from de docs...
CSVReader(Reader reader, char separator)
Constructs CSVReader with supplied separator.
If there is comma in columns, it gets difficult to read a csv file properly, I will show you the code that doesn't requires any library:
File f=new File("C:/Users/Public/"+filename+".csv");
Scanner sc=new Scanner(fc);
sc.useDelimiter("\r");
char cd='"';
//Create scanner string seperator ","
String g=String.valueOf(cd)+","+String.valueOf(cd);
//JOptionPane.showMessageDialog(null, "G: "+g);
String[] data=null;
while(sc.hasNextLine()){
data=null;
csvtemp=sc.nextLine().toString();
//remove first character ", string from second character
csvtemp=csvtemp.subString(1);
//remove last character ", string from first character to secondlast
csvtemp=csvtemp.subString(0,csvtemp.length()-1);
//Split record by seperator "," and copy to array
data=csvtemp.split(g);
}
sc.close();

Word counter program not producing correct number of words

I'm new to reading text from a file.
I've got a task for which I need to print the amount of words which are in a file.
I'm using TextEdit on mac OS which ends in .rtf
When I run the following program, I get the output 5 even when the document is empty. When I add words, the count doesn't increment correctly.
Thanks.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Analyze{
public static void main(String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
int words = 0;
System.out.println("This is a word counter");
System.out.println("File name");
String filename = console.next();
File name = new File(filename);
Scanner int2 = new Scanner(name);
while (int2.hasNext()) {
String temp = int2.next();
words++;
}
System.out.println(words);
}
}
The problem is that you are reading a RTF file.
A 'blank' (as in no entered text) RTF file generated with TextEdit looks like this:
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf130
{\fonttbl}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
}
As you can see, the five lines correspond to the output of 5.
Either parse RTF in your program, which I doubt you want to do, or switch TextEdit to plaintext mode. See here
The file you're trying to count is an RTF file? Does it support italics, bold, font selection and things like that? In that case, it probably contains some data, even if there is no text. Your program does not care about the file format, so it naïvely reads everything as text.
Try running od or hexdump on your file (not sure if these exist on Mac OS X?) -- they print the exact bytes of a file. A truly empty file should not yield any output.
If your computer doesn't have the od or hexdump programs, you could try cat. It doesn't print the contents as numbers, so it doesn't give a 100% accurate view of special characters, but it should be able to demonstrate to you whether your file is empty or not.
Besides the RTF-Problem, also note that
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
with whitespace as in
A whitespace character: [ \t\n\x0B\f\r]
so the count is including tabs, newlines, etc. not only blanks

Trying to read binary file as text but scanner stops at first line

I'm trying to read a binary file but my program just stops at first line..
I think it's because of the strange characters the file has..I just want to extract some directions from it. Is there a way to do this?..
public static void main(String[] args) throws IOException
{
Scanner readF = new Scanner(new File("D:\\CurrentDatabase_372.txt"));
String line = null;
String newLine = System.getProperty("line.separator");
FileWriter writeF = new FileWriter("D:\\Songs.txt");
while (readF.hasNext())
{
line = readF.nextLine();
if (line.contains("D:\\") && line.contains(".mp3"))
{
writeF.write(line.substring(line.indexOf("D:\\"), line.indexOf(".mp3") + 4) + newLine);
}
}
readF.close();
writeF.close();
}
The file starts like this:
pppppamepD:\Music\Korn\Untouchables\03 Blame.mp3pmp3pmp3pKornpMetalpKornpUntouchablespKornpUntouchables*;*KornpKornpKornUntouchables003pMetalKornUntouchables003pBlameKornUntouchables003pKornKornUntouchables003pMP3pppppCpppÀppp#ppøp·pppŸú#pdppppppòrSpUpppppp€ppªp8›qpppppppppppp,’ppÒppp’ÍpET?ppppppôpp¼}`Ñ#ãâK†¡H¤*(DppppppppppppppppuÞѤéú:M®$#]jkÝW0ÛœFµú½XVNp`w—wâÊp:ºŽwâÊpppp8Npdpp¡pp{)pppppppppppppppppyY:¸[ªA¥Bi `Û¯pppppppppppp2pppppppppppppppppppppppppppppppppppp¿ÞpAppppppp€ppp€;€?€CpCpC€H€N€S€`€e€y€~p~p~€’€«€Ê€â€Hollow LifepD:\Musica\Korn\Untouchables\04 Hollow Life.mp3pmp3pmp3pKornpMetalpKornpUntouchablespKornpUntouchables*;*KornpKornpKornUntouchables004pMetalKornUntouchables004pHollow LifeKornUntouchables004pKornKornUntouchables004pMP3pppppCpppÀHppppppøp¸pppǺxp‰ppppppòrSpUpppppp€ppªp8›qpppppppppppp,’ppÒpppŠºppppppppppôpp¼}`Ñ#ãâK†¡H¤*(DpppppppppppppppppãG#™R‚CA—®þ^bN °mbŽ‚^¨pG¦sp;5p5ÓÐùšwâÊp
)ŽwâÊpppp8Npdpp!cpp{pppppppppppppppppyY:¸[ªA¥Bi `ۯǺxp‰pppppp2pppppppppppppppppppppppppppppppppppp¿
I want to extract file directions like "D:\Music\Korn\Untouchables\03 Blame.mp3".
You cannot use a line-oriented scanner to read binary files. You have no guarantee that the binary file even has "lines" delimited by newline characters. For example, what would your scanner do if there were TWO files matching the pattern "D:\.*.mp3" with no intervening newline? You would extract everything between the first "D:\" and the last ".mp3", with all the garbage in between. Extracting file names from a non-delimited stream such as this requires a different strategy.
If i were writing this I'd use a relatively simple finite-state recognizer that processes characters one at a time. When it encounters a "d" it starts saving characters, checking each character to ensure that it matches the required pattern, ending when it sees the "3" in ".mp3". If at any point it detects a character that doesn't fit, it resets and continues looking.
EDIT: If the files to be processed are small (less than 50mb or so) you could load the entire file into memory, which would make scanning simpler.
As was said, since it is a binary file you can't use a Scanner or other character based readers. You could use a regular FileInputStream to read the actual raw bytes of the file. Java's String class has a constructor that will take an array of bytes and turn them into a string. You can then search that string for the file name(s). This may work if you just use the default character set.
String(byte[]):
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
FileInputStream for reading bytes:
http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html
Use hasNextLine() instead of hasNext() in the while loop check.
while (readF.hasNextLine()) {
String line = readF.nextLine();
//Your code
}

Categories