How to write a String to fixed-size text files in Java? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a very long string and I want to write it to several text files of fixed size. For example, I want to set the size to be 1MB per file, and label each file as "text01.txt", "text02.txt"...
How can I achieve this in the simplest way?

Keep track of the number of bytes you're writing, and when it reaches a specified point, close the existing file and continue in a new one. There's no need to analyze the size of the file, since you know exactly what's going into it.
Something like this:
long fileSizeByteLimit = 5000000;
long bytesOutput = 0;
while(THEREAREMORELINESTOOUTPUT) {
//Open a new file
while(bytesOutput <= fileSizeByteLimit) {
writer.append(lineOfOutput);
bytesOutput += lineOfOutput.length();
}
//Close file
}

Related

how to split a text file by line gaps in java [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 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.

Java linking int value to a random generated code [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'm trying to build simple java net cafe timer. I done random code generator. Now i need to be able to add custom time amount to every code or to generate multiple codes with same amount of time i.e. 30 codes with 30 min time. Code is created as hexadecimal values.
SO it should be something like this
1EEE has 30 minutes
CDB9 has 60 minutes
and so on
Latter i will implement client/server, and user will be able to use computer for the time he/she has on time code.
Code for generating time codes:
public String createRandomTimeCode(int length) {
Random random = new Random();
StringBuilder code = new StringBuilder();
while (code.length() < length) {
code.append(Integer.toHexString(random.nextInt()));
}
String Short = code.substring(0,4);
return Short.toString();
I wasn't clear. My question was how to store different values like time codes + amount of time on that code. And i got the answer in comments.
What your looking for is HashMap

Using split to store information from .txt file into array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a file called log.txt (in same directory as the program), and it contains data which I want to split based on . and store it into String[] plan.
e.g., The log.txt contains a string like 332 445.114 554.963 342. and so on...
What I want is to split it in such a way so that:
plan[0]=332 445;
plan[1]=114 554;
plan[2]=963 342;
And so on...
How about this:
String[] plan = (new Scanner( new File("log.txt") ).useDelimiter("\\A").next()).split("[\\r\\n]+");
This line saves lines from file into an array of String.
Is it okay for you?
Edit: Here is what you might be looking for...
String[] plan = (new Scanner( new File("log.txt") ).useDelimiter("\\A").next()).split("\\.");

How to know file size from bytes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have byte array, how to know file size ? (in JAVA)
File file = new File("patch");
file.length() // <--- it's good, but I haven't original file... (( I get file in bytes from DataBase !
Thanks
You have an array with you and every array has a length. That's it.
byteArray.length;
And
1kb = 1024 bytes

What is a buffer on java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a homework to do on Java and is asks me to create a buffer method and the constructor should make an empty buffer structure.
There is no details about what that buffer is. It also wants to insert chars inside the buffer, delete the char that buffer shows, go buffer X positions left or right and tell the number of buffers chars. All these with different methods.
The problem is WHAT IS THAT BUFFER??? Is this something specific?
I would just use a StringBuilder. There are many other possible solutions but StringBuilder is the most widely used buffer for chars these days.
StringBuilder sb = new StringBuidler();
It also wants to insert chars inside the buffer,
sb.append('!');
sb.append("Hello");
sb.insert(5, "bye");
delete the char that buffer shows,
sb.delete(3, 6);
go buffer X positions left or right and tell the number of buffers chars.
sb.charAt(5); // character at 5
int len = sb.length(); // number of characters.

Categories