how to split a text file by line gaps in java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a text file in Java that looks like this,
"
Q1. You are given a train data set having 1000 columns and 1 million rows. The data set is based on a classification problem. Your manager has asked you to reduce the dimension of this data so that model computation time can be reduced. Your machine has memory constraints. What would you do? (You are free to make practical assumptions.)
Q2. Is rotation necessary in PCA? If yes, Why? What will happen if you don’t rotate the components?
Q3. You are given a data set. The data set has missing values which spread along 1 standard deviation from the median. What percentage of data would remain unaffected? Why? "
Now, I want to read this file and then store each of these sentences(questions) in a string array. How can I do that in java?
I tried this,
String mlq = new String(Files.readAllBytes(Paths.get("MLques.txt")));
String[] mlq1=mlq.split("\n\n");
But this is not working.

Try this
String mlq = new String(Files.readAllBytes(Paths.get("MLQ.txt")));
String[] mlq1=mlq.split("\r\n\r\n");
System.out.println(mlq1.length);
System.out.println(Arrays.toString(mlq1));
This should do it by line gap of 2 lines.

File file = new File("C:\\MLques.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st + "\n");
}
I think it will work.

This is a piece of code from one of my project.
public static List<String> readStreamByLines(InputStream in) throws IOException {
return IOUtils.readLines(in, StandardCharsets.UTF_8).stream()
.map(String::trim)
.collect(Collectors.toList());
}
But!!! If you have really big file, then collecting all content into a List is not good. You have to read InputStream line by line and do all you need for every single row.

Related

Use of java.txt files, polymorphism and delimiters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I need to read from a text file in java and pass that information through a polymorphic method. My idea is a CryptoWallet in a .txt file reading as coin, amount and value, where in the text file its represented as Bitcoin 100 1.25.
Ive got the code reading from the file and printing it, as below.
public class CryptoCurrencies
public static void main (String[] args) throws IOException
{
System.out.println("Welcome to your Crypto Wallet"
+ "\nCurrently, you only own one coin.");
File CryptoWallet = new File("/Users/curti/OneDrive/Desktop/crypto.txt");
Scanner scan = new Scanner(CryptoWallet);
String fileContent = " ";
while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
continue;
}
My main issue is actually getting the text file too recognise the numbers as doubles and variables, and assigning them to run through a polymorphic method. I understand the polymorphism side, but if anyone has any ideas for a possible polymorphic method id really appreciate it, having trouble thinking at the moment!
Thanks everyone.
It is unclear what role polymorphism is supposed to have here.
Assuming that each row represents a record and each record has the same form then we can parse each row in an identical fashion. That allows you to extract the numeric values using the parse methods in the appropriate Number classes.
For instance...
while(scan.hasNextLine()) {
String line = scan.nextLine());
String[] tokens = line.split(" ");
String name = tokens[0];
int amount = Integer.parse(tokens[1]);
double value = Double.parse(tokens[2]);
}
p.s. obligatory comment that you shouldn't represent money in floating point variables.
Split the current line by space or "\s+" regex, check if the length is 3, then use the double.parse function from the double object on the 2nd and 3rd element in the array.

Java the best and fastest way to edit text file [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need to edit a lot of text files effectively and fast! What is the best thing I can do?
I already come up with this function:
private boolean edit(File source)
{
if (!source.getAbsolutePath().endsWith(".java")) //Java text files only
return false;
String l, str = "", orig = "";
try
{
BufferedReader r = new BufferedReader(new FileReader(source));
while ((l = r.readLine()) != null)
{
orig = str += l+"\n";
}
r.close();
for (Entry<String, String> e : mappings.entrySet()) //Replacing string by HashMap mappings!
str = fastReplace(str, e.getKey(), e.getValue()); //Faster alterntive to String#replaceAll
if (!str.equals(orig))
{
BufferedWriter bf = new BufferedWriter(new FileWriter(source));
bf.write(str);
bf.close();
return true;
}
}
catch (Exception e)
{
doLog(e.toString()); //Logging exception but unimportant for us...
}
return false;
}
I found my function a little bit clumsy because it first needs to read text file into string then edit it and write it back after that. So the question is. Is there any better and faster way to edit text file? I mean for example without necessity to turning it into string and then writing it back. For example is there a way to edit file directly as a text file or writing it without overriding the same unchanged parts of the file or any faster way to read and write file? Or is my function actually already fastest as it can be?
In case somebody is wondering what my "fastReplace" function does then check this Faster alternatives to replace method in a Java String? but I do not think it is important.
If you need to replace a string by another one of the exact same size byte for byte, then you could read the data sequentially in chunks with multiple block size, replace the desired spots and write the data back if a change has been made. If no change was made then there is no need to write back the block. In the best scenario, you will save few I/O operations, at the cost of significant code complexity.
If your editing is more complex and involves string insertions, then you have no way out of reading and writing back the entire text.
Early optimization is a bad idea. Source code files hardly span a single block and in your case you will probably save no time or space.

Handle data from file having big size in java [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 6 years ago.
Improve this question
I have file filled with text lines. File's size is about 150GB. What the best solution to sort all of the lines by alphabet? For example:
INPUT:
We’re pretty sure someone famous once said that. breathe in the
millennial generation. Anyway, It’s the phrase that we live by
here at Alphabet. have an unrivalled energy and passion for
creating brands that live and
OUTPUT:
Anyway, It’s the phrase that we live by here at Alphabet. breathe
in the millennial generation. have an unrivalled energy and
passion for creating brands that live and We’re pretty sure
someone famous once said that.
We usually don’t need all of the lines in the file in memory at once – instead, we just need to be able to iterate through each one, do some processing and throw it away. So, this is exactly what we’re going to do – iterate through the lines without holding the in memory.
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
This solution will iterate through all the lines in the file – allowing for processing of each line – without keeping references to them – and in conclusion, without keeping them in memory. http://www.baeldung.com/java-read-lines-large-file

How to read previous line by using bufferedReader [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a some large project. In that I am stuck at one place. I am using the following code to read from the file:
String str = null;
FileReader fr = new FileReader("H:\\Eclipse\\Emulator\\progin8085.txt");
BufferedReader br = new BufferedReader(fr);
str = br.readLine(); //using it in a loop from 0 to total_no_of_lines
Now I when I reached suppose at line 8(counting lines numbers while entering data to the file), I want to go back to line 3 or 4 or any and again want to read and execute each statement. How to read previous statements using BufferedReader only? If it is not possible, any other solution?
Some Readers support marks. You can use those to rewind the file. A particularly useful Reader (imo) is the LineNumberReader and it supports marks. This sort of code might suit your needs.
public static int final READ_AHEAD_LIMIT = 100000;
LineNumberReader lnReader = new LineNumberReader(reader);
while (youWantToRead) {
...
if (mightBeInterestingLater) {
lnReader.mark(READ_AHEAD_LIMIT);
}
...
if (nowWantToRewind) {
lnReader.reset();
// We're now at whatever place mark() was last called at.
}
Save all the lines of text file in String[] StrArray and do whatever you want.

Moving from buffer to an array for manipulation in Java [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 am making a program to take data from a .txt document, I have used the BufferedReader method which goes through the document. It then prints the the documents contents into the console. I cannot however for the life of me figure out how to get the contents from the BufferedReader into a string array so I can then further manipulate it. Any help?
The method Files.readAllLines reads a file into a List<String>, one String per line in the original file. The method Files.readAllBytes just reads a file into a byte array if that's your style. These are both part of the Java NIO 2 library, part of Java SE 7.
Path myFile = Paths.get("hello_world.txt");
List<String> fileLines = Files.readAllLines(myFile);
use DataInputStream instead of BufferedReader
DataInputStream is = new DataInputStream (new FileInputStream ("myFileName"));
then you can store a String with methtod call is.readUTF ();
If you want a String[] array containing the lines of the document, you could use BufferedReader.readLine(), and stuff the lines into a List<String> instead of printing them.
Here's how you can create a list of Strings:
List<String> lines = new ArrayList<String>();
And here's how you can add to it:
String line = null;
while( (line = reader.readLine()) != null ) {
lines.add( line );
}

Categories