Failed to remove the header of multiple csv files - java

I want to remove the header of multiple csv files. So when I am trying to do this it's showing error. But I am able to remove the single csv file's header by this way.
What I have missed to achieve my target that I can remove multiple csv files's header at one shot? I need help on this.
Note: I have given correct filename, directory name, or volume label syntax.
package hadoop;
import java.io.IOException;
import java.io.RandomAccessFile;
class RemoveLine
{
public static void main(String...args) throws IOException
{
RandomAccessFile raf = new RandomAccessFile("F://sample1/*.csv", "rw");
//Initial write position
long writePosition = raf.getFilePointer();
raf.readLine();
// Shift the next lines upwards.
long readPosition = raf.getFilePointer();
byte[] buff = new byte[1024];
int n;
while (-1 != (n = raf.read(buff))) {
raf.seek(writePosition);
raf.write(buff, 0, n);
readPosition += n;
writePosition += n;
raf.seek(readPosition);
}
raf.setLength(writePosition);
raf.close();
}
}
Output:
Exception in thread "main" java.io.FileNotFoundException: F:\sample1\*.csv (The filename, directory name, or volume label syntax is incorrect)
at java.io.RandomAccessFile.open0(Native Method)
at java.io.RandomAccessFile.open(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at java.io.RandomAccessFile.<init>(Unknown Source)
at hadoop.RemoveLine.main(RemoveLine.java:12)

You are probably thinking of glob syntax which you use at the command line. Windows cmd and Linux bash takes something like *.csv and expands it into a list of all the matching file names.
On the other hand, Java's RandomAccessFile expect a specific file name and does not understand glob syntax. You must implement the behavior yourself. First you need to get a list of all the files which you want to change. Then you have to iterate over that list and perform the actions you want.

Related

How to format and compare input from a bash/vm?

I'm working on a Desktop Application which will analyze some stuff on a virtual machine.
I get to write and read after the SSH connection.
I save the input in a temporary .txt file. Now I want to compare it to let's say another .txt file which contains the correct amount/name of directories.
For example if I want to check for the directories on ../workspace/ with my below code from scanForVm. The content (actually not only the command output) is saved to a .txt file.
I have the problem that it writes some unnecessary stuff into it and I'm unable to compare it with my other .txt file.
I tried to search for an answer for several hours now and tried different approaches.
public void scanForVm(MouseEvent event) throws IOException, InterruptedException {
if (event.getSource() == scanButton) {
networkCheck.scanNetwork();
sshConnection.connectionToSsh("ls -d workspace/*/\n");
sshConnection.readOutput();
}
}
and then:
public void readOutput() throws IOException, InterruptedException {
Path file = Paths.get(" / path / To / Text.txt ");
System.out.println(outputDescr); //Debug format
InputStream stdout = new StreamGobbler(sess.getStdout());
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
String output = "";
while ((read = stdout.read(buffer)) != -1) {
result.write(buffer, 0, read);
Files.write(file, buffer);
Thread.sleep(2000);
System.out.println(output);
}
}
I want the input from the vm to be actual readable.
Now I get this:
Identity added:
/path/path/path/.ssh/...
[0m[01;36mworkspace/directory/[0m [01;36mworkspace/directory2/[0m
ls -d workspace/*/
And I want it to be only:
workspace/directory/
workspace/directory2/
[0m and [01;36m are colour identifiers. [0m signifies no colour, [01;36m signifies light cyan. They could be parsed out using a regex like \[(0|\d+;\d+)m.
In your specific example, everything that starts out setting the colour to light cyan and back to Standard at the end could be found using \[01;36m(.+?)\[0m with group 1 containing the desired string. You can test it here: https://regex101.com/r/A906Na/1

What is a good way to load many pictures and their reference in an array? - Java + ImageJ

I have for example 1000 images and their names are all very similar, they just differ in the number. "ImageNmbr0001", "ImageNmbr0002", ....., ImageNmbr1000 etc.;
I would like to get every image and store them into an ImageProcessor Array.
So, for example, if I use a method on element of this array, then this method is applied on the picture, for example count the black pixel in it.
I can use a for loop the get numbers from 1 to 1000, turn them into a string and create substrings of the filenames to load and then attach the string numbers again to the file name and let it load that image.
However I would still have to turn it somehow into an element I can store in an array and I don't a method yet, that receives a string, in fact the file path and returns the respective ImageProcessor that is stored at it's end.
Also my approach at the moment seems rather clumsy and not too elegant. So I would be very happy, if someone could show me a better to do that using methods from those packages:
import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
I think I found a solution:
Opener opener = new Opener();
String imageFilePath = "somePath";
ImagePlus imp = opener.openImage(imageFilePath);
ImageProcesser ip = imp.getProcessor();
That do the job, but thank you for your time/effort.
I'm not sure if I undestand what you want exacly... But I definitly would not save each information of each image in separate files for 2 reasons:
- It's slower to save and read the content of multiple files compare with 1 medium size file
- Each file adds overhead (files need Path, minimum size in disk, etc)
If you want performance, group multiple image descriptions in single description files.
If you dont want to make a binary description file, you can always use a Database, which is build for it, performance in read and normally on save.
I dont know exacly what your needs, but I guess you can try make a binary file with fixed size data and read it later
Example:
public static void main(String[] args) throws IOException {
FileOutputStream fout = null;
FileInputStream fin = null;
try {
fout = new FileOutputStream("description.bin");
DataOutputStream dout = new DataOutputStream(fout);
for (int x = 0; x < 1000; x++) {
dout.writeInt(10); // Write Int data
}
fin = new FileInputStream("description.bin");
DataInputStream din = new DataInputStream(fin);
for (int x = 0; x < 1000; x++) {
System.out.println(din.readInt()); // Read Int data
}
} catch (Exception e) {
} finally {
if (fout != null) {
fout.close();
}
if (fin != null) {
fin.close();
}
}
}
In this example, the code writes integers in "description.bin" file and then read them.
This is pretty fast in Java, since Java uses "channels" for files by default

Insert a string in the middle of text file without replacing [duplicate]

This question already has answers here:
inserting data in the middle of a text file through java
(2 answers)
Closed 9 years ago.
Suppose i have a text file named Sample.text.
i need advice on how to achieve this:
Sample.txt before running a program:
ABCD
while running the program, user will input string to be added starting at the middle
for example: user input is XXX
Sample.txt after running a program:
ABXXXCD
Basically you've got to rewrite the file, at least from the middle. This isn't a matter of Java - it's a matter of what file systems support.
Typically the way to do this is to open both the input file and an output file, then:
Copy the first part from the input file to the output file
Write the middle section to the output file
Copy the remainder of the input file to the output file
Optionally perform file renaming if you want the new file to have the same eventual name as the original file
The basic idea is to read the file contents into memory, say at program start, manipulate the string as desired, then write the entire thing back to the file.
So you would open and read in Sample.txt. In memory you have a string = "ABCD"
in your program execution, accept user input of XXX. Insert that into your string with your favorite string manipulation method. Now string = "ABXXXCD"
Finally you would overwrite Sample.txt with your updated string and close it.
If you were worried about corruption or something, you might save it to a secondary file, then verify its contents, delete the original, and rename the new to be the same as the original.
Actually i have did something like what you want, here try this code, its not a complete but it should give you a clear idea:
public void addString(String fileContent, String insertData) throws IOException {
String firstPart = getFirstPart(fileContent);
Pattern p = Pattern.compile(firstPart);
Matcher matcher = p.matcher(fileContent);
int end = 0;
boolean matched = matcher.find();
if (matched) {
end = matcher.end();
}
if(matched) {
String secondPart = fileContent.substring(end);
StringBuilder newFileContent = new StringBuilder();
newFileContent.append(firstPart);
newFileContent.append(insertData);
newFileContent.append(secondPart);
writeNewFileContent(newFileContent.toString());
}
}
Normally a new file would be created, but the following probably suffices (for non-gigabyte files). Mind the explicit encoding UTF-8; which you can ommit for the encoding of the operating system.
public static void insertInMidstOfFile(File file, String textToInsert)
throws IOException {
if (!file.exists()) {
throw new FileNotFoundException("File not found: " + file.getPath());
// Because file open mode "rw" would create it.
}
if (textToInsert.isEmpty()) {
return;
}
long fileLength = file.length();
long startPosition = fileLength / 2;
long remainingLength = fileLength - startPosition;
if (remainingLength > Integer.MAX_VALUE) {
throw new IllegalStateException("File too large");
}
byte[] bytesToInsert = textToInsert.getBytes(StandardCharsets.UTF_8);
try (RandomAccessFile fh = new RandomAccessFile(file, "rw")) {
fh.seek(startPosition);
byte[] remainder = new byte[(int)remainingLength];
fh.readFully(remainder);
fh.seek(startPosition);
fh.write(bytesToInsert);
fh.write(remainder);
}
}
Java 7 or higher.

How can i return the position of a line in a Random Access file in Java?

I create a random access file and I write some info in it.
I would like to store in an array the position of each line in the random access file.
I'm going to have an index which points to every line of the random access file.
So I need to store in my index the positions of the lines the random access file.
My program is as follows
package randomaccessfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class raf {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("DocumentsFile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
for (int i = 0; i <= 10 ; i++) {
String sentence = "1 Info Name Surname"; //i create sentences in the random access file
raf.seek(file.length());
raf.writeBytes(sentence);
raf.writeBytes("\r\n");
}
raf.close();
}
}
For each of the lines that are created in the Random access file,
I would like to store in an array their positions.
The positions then are going to be stored in the index.
Is there any method that I can use in order to return the position of a line
in the random access file?
RandomAccessFile already provides everything you want.
For one, you don't need seek() here: the file pointer advances past the bytes written into the file on each write operation.
This means that, after you have written line n, for whatever value of n, grabbing the result of .getFilePointer() from the RandomAccessFile object will give you the starting offset of line n+1. Line 1 starts at offset 0 and that's pretty much the only thing you have to really account for here.
On the other hand, if what you really care about are line numbers, you could use a LineNumberReader to map every line in the written file to a given line number.
File file = new File("jedi.txt");
try(LineNumberReader lnr = new LineNumberReader(new FileReader(file))){
String line = null;
while ( (line = lnr.readLine()) != null){
System.out.println(lnr.getLineNumber() + " " + line );
}
}catch(IOException e){
e.printStackTrace();
}
Of course this has nothing to do with RandomAccessFiles, but it could be an alternative depending on what you are doing.
You can use a temporary variable to store the last FilePointer. If the readline must be reversed, you can set the reader pointer using this variable.
RandomAccessFile myReader = new RandomAccessFile(fileObject, "r");
long current_postion = myReader.getFilePointer();
myReader.readLine();
if (line should be reversed){
myReader.seek(current_postion); //Point to the previous line
}

Inserting text into an existing file via Java

I would like to create a simple program (in Java) which edits text files - particularly one which performs inserting arbitrary pieces of text at random positions in a text file. This feature is part of a larger program I am currently writing.
Reading the description about java.util.RandomAccessFile, it appears that any write operations performed in the middle of a file would actually overwrite the exiting content. This is a side-effect which I would like to avoid (if possible).
Is there a simple way to achieve this?
Thanks in advance.
Okay, this question is pretty old, but FileChannels exist since Java 1.4 and I don't know why they aren't mentioned anywhere when dealing with the problem of replacing or inserting content in files. FileChannels are fast, use them.
Here's an example (ignoring exceptions and some other stuff):
public void insert(String filename, long offset, byte[] content) {
RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");
long fileSize = r.length();
FileChannel sourceChannel = r.getChannel();
FileChannel targetChannel = rtemp.getChannel();
sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
sourceChannel.truncate(offset);
r.seek(offset);
r.write(content);
long newOffset = r.getFilePointer();
targetChannel.position(0L);
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
sourceChannel.close();
targetChannel.close();
}
Well, no, I don't believe there is a way to avoid overwriting existing content with a single, standard Java IO API call.
If the files are not too large, just read the entire file into an ArrayList (an entry per line) and either rewrite entries or insert new entries for new lines.
Then overwrite the existing file with new content, or move the existing file to a backup and write a new file.
Depending on how sophisticated the edits need to be, your data structure may need to change.
Another method would be to read characters from the existing file while writing to the edited file and edit the stream as it is read.
If Java has a way to memory map files, then what you can do is extend the file to its new length, map the file, memmove all the bytes down to the end to make a hole and write the new data into the hole.
This works in C. Never tried it in Java.
Another way I just thought of to do the same but with random file access.
Seek to the end - 1 MB
Read 1 MB
Write that to original position + gap size.
Repeat for each previous 1 MB working toward the beginning of the file.
Stop when you reach the desired gap position.
Use a larger buffer size for faster performance.
You can use following code:
BufferedReader reader = null;
BufferedWriter writer = null;
ArrayList list = new ArrayList();
try {
reader = new BufferedReader(new FileReader(fileName));
String tmp;
while ((tmp = reader.readLine()) != null)
list.add(tmp);
OUtil.closeReader(reader);
list.add(0, "Start Text");
list.add("End Text");
writer = new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i < list.size(); i++)
writer.write(list.get(i) + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
OUtil.closeReader(reader);
OUtil.closeWriter(writer);
}
I don't know if there's a handy way to do it straight otherwise than
read the beginning of the file and write it to target
write your new text to target
read the rest of the file and write it to target.
About the target : You can construct the new contents of the file in memory and then overwrite the old content of the file if the files handled aren't so big. Or you can write the result to a temporary file.
The thing would probably be easiest to do with streams, RandomAccessFile doesn't seem to be meant for inserting in the middle (afaik). Check the tutorial if you need.
I believe the only way to insert text into an existing text file is to read the original file and write the content in a temporary file with the new text inserted. Then erase the original file and rename the temporary file to the original name.
This example is focused on inserted a single line into an existing file, but still maybe of use to you.
If it is a text file,,,,Read the existing file in StringBuffer and append the new content in the same StringBuffer now u can write the SrtingBuffer on file. so now the file contains both the existing and new text.
As #xor_eq answer's edit queue is full, here in a new answer a more documented and slightly improved version of his:
public static void insert(String filename, long offset, byte[] content) throws IOException {
File temp = Files.createTempFile("insertTempFile", ".temp").toFile(); // Create a temporary file to save content to
try (RandomAccessFile r = new RandomAccessFile(new File(filename), "rw"); // Open file for read & write
RandomAccessFile rtemp = new RandomAccessFile(temp, "rw"); // Open temporary file for read & write
FileChannel sourceChannel = r.getChannel(); // Channel of file
FileChannel targetChannel = rtemp.getChannel()) { // Channel of temporary file
long fileSize = r.length();
sourceChannel.transferTo(offset, (fileSize - offset), targetChannel); // Copy content after insert index to
// temporary file
sourceChannel.truncate(offset); // Remove content past insert index from file
r.seek(offset); // Goto back of file (now insert index)
r.write(content); // Write new content
long newOffset = r.getFilePointer(); // The current offset
targetChannel.position(0L); // Goto start of temporary file
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset)); // Copy all content of temporary
// to end of file
}
Files.delete(temp.toPath()); // Delete the temporary file as not needed anymore
}

Categories