I have on bucket XLS file and I have to pull the file and read the data on the stream and work with data. I worked with CSV file and this is my code:
try (ReadChannel reader = storage.reader(bucketName, fileName)) {
ByteBuffer bytes = ByteBuffer.allocate(BUFFER_SIZE);
while (reader.read(bytes) > 0) {
bytes.flip();
// outChannel.write(bytes);
set(new String(bytes.array(), "UTF-8"), fileName);
bytes.clear();
}
}
I think this might be what you are looking for. GCS Input Channel
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename fileName = new GcsFilename("TestBucket", "Test1.xlsx");
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, BUFFER_SIZE);
InputStream inputStream = Channels.newInputStream(readChannel);
I've also found an interesting workaround that might help you, in this answer you have a piece of code that converts all the excel files to CSV to let you manipulate them as normal if you wish to.
EDIT: The error was there because it was not initialized. Have a look at my code edit.
In your case as far as I understood you might need to use "OutputChannel" not "InputChannel" but it's the same concept.
Hope this helps.
Related
I am a freshman in Neo4J. I think I am also a freshman in Java though I have learn it for neary 2 years.
I want to save and read a picture in neo4j database, I have a InputStream instance, Its cotent is a picture data. I have a Resoucre Object. it has a byte[] property used to save the picture data. so I do that
public static Resource getResourceInstance(InputStream in, String title) throws IOException{
StringBuilder sb = new StringBuilder();
BufferedInputStream input = new BufferedInputStream(in);
int b;
while((b = input.read()) != -1){
sb.append(b);
}
input.close();
in.close();
return new Resource(sb.toString().getBytes(), title, 0, 0);
}
then I use a transaction to save it to neo4j. and I check it by neo4j-server. in database, the byte array is number like 51,52,45 and so on
the second step I want to read the byte array from database.
I put it in Resource Object. and use FileOutputStream read it the code like this
images = resource.getImage();
String titleString = resource.getTitle();
String path = "images" + File.separator + titleString + ".jpg";
System.out.println(Paths.get(path).toRealPath());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(path)));
out.write(images);
out.close();
this is a Java web project.
I don't know why I have to create a file in path(String path = "images" + File.separator + titleString + ".jpg";) at first.
though I do so, I can't open the file like a picture.
I am very dispirited now. and I don't konw how to do. can you help me?
thank you very much.
PS:
my english is poor,bet your tolerating.
don't do this in the first place.
store the picture on a filesystem or a public storage like s3, dropbox etc. and save the url or filename in the neo4j property.
If you want to read a file into a byte[] create an array of the file.length() size and read into that array using the right offset until is. read() returns -1
Is it possible to read whole partition of disk in java?
Partition is treated as directory (obviously) and because of it I can't create FileInputStream which I need.
I'd like to compute hash for whole partition of disk (byte by byte) and I was wondering how to achieve that.
If that matters it has to work both on Windows and Linux.
Any thoughts are appreciated!
Try that with administrator's permissions:
File diskRoot = new File ("\\.\X:"); //X is your requested partition's letter
RandomAccessFile diskAccess = new RandomAccessFile(diskRoot, "r");
byte[] content = new byte[1024];
diskAccess.readFully (content);
You can also use BufferedInputStream. It's the hunsricker's answer from How to access specific raw data on disk from java
The naming convention can be found here: http://support.microsoft.com/kb/100027
As #Michal suggested I've changed path to partition to "\\.\X:" convention.
Code looks like this:
File diskRoot = new File("\\\\.\\F:");
byte[] buffer = new byte[1024];
try (InpuStream stream = new FileInputStream(diskRoot)) {
while(stream.read(buffer) != -1) {
//do something with buffer
}
} catch(IOException e) {
//handle exception
}
Thanks for the comments and answers!
I try to send multiple Files from my Server (NanoHttpd) to my Client (Apache DefaultHttpClient).
My approach is to send multiple files via one Response of NanoHttpd.
For this purpose i wanted to use SequenceInputStream.
I am trying to concatenate multiple Files, send them via the Response (InputStream) and write every File again in a seperate File with my Client.
On the Serverside i call this:
List<InputStream> data = new ArrayList<InputStream>(o_file_path.size());
for (String file_name : files)
{
File file = new File(file_name);
data.add(new FileInputStream(file));
}
InputStream is = new SequenceInputStream(Collections.enumeration(data));
return new NanoHTTPD.Response(HTTP_OK, "application/octet-stream", is);
Now my Question is how to receive and split the Files correctly.
I have tried it this way on my client, but it does not work:
int read = 0;
int remaining = 0;
byte[] bytes = new byte[buffer];
// Read till the end of the Stream
while ( (read != -1) && (counter < files.size()))
{
// Create a .o file for the current file
read = 0;
remaining = is.available();
// Should open each Stream
while (remaining > 0)
{
read = is.read(bytes);
remaining = remaining - read;
os.write(bytes, 0, read);
}
os.flush();
os.close();
}
This way I want to go over all Stream (untill read == 1, or i know there is no file anymore), and read any stream into a file.
I clearly seem to understand something groundbreaking wrong, since is.available() always is 0.
Could anyone please tell me how to read properly from this SequencedInputStream, or how to solve my Problem.
Thanks in advance.
It won't work this way. SequenceInputStream will merge all input streams in one solid byte stream. There will be no separators or EOFs. I suggest to abandon the idea and look for a different approach.
I know that there are some similar questions in the site, but they could not provide me a helpful answer. What is the best/most efficient way to read a .bin file in Java line by line? Which classes and methods should someone use to open it and get the data? Could Bufferedreader do the job or is it only for text files;
Binary file don't have lines, but you must know the format of the file to know what structure exists (headers, structs,etc) and write a parser.
You can use BufferedInputStream, see the following:
http://www.javapractices.com/topic/TopicAction.do?Id=245
Read structured data from binary file -?
This should do it.
public byte[] readFromStream(InputStream inputStream) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] data = new byte[4096];
int count = inputStream.read(data);
while(count != -1)
{
dos.write(data, 0, count);
count = inputStream.read(data);
}
return baos.toByteArray();
}
I need to write something into a text file's beginning. I have a text file with content and i want write something before this content. Say i have;
Good afternoon sir,how are you today?
I'm fine,how are you?
Thanks for asking,I'm great
After modifying,I want it to be like this:
Page 1-Scene 59
25.05.2011
Good afternoon sir,how are you today?
I'm fine,how are you?
Thanks for asking,I'm great
Just made up the content :) How can i modify a text file like this way?
You can't really modify it that way - file systems don't generally let you insert data in arbitrary locations - but you can:
Create a new file
Write the prefix to it
Copy the data from the old file to the new file
Move the old file to a backup location
Move the new file to the old file's location
Optionally delete the old backup file
Just in case it will be useful for someone here is full source code of method to prepend lines to a file using Apache Commons IO library. The code does not read whole file into memory, so will work on files of any size.
public static void prependPrefix(File input, String prefix) throws IOException {
LineIterator li = FileUtils.lineIterator(input);
File tempFile = File.createTempFile("prependPrefix", ".tmp");
BufferedWriter w = new BufferedWriter(new FileWriter(tempFile));
try {
w.write(prefix);
while (li.hasNext()) {
w.write(li.next());
w.write("\n");
}
} finally {
IOUtils.closeQuietly(w);
LineIterator.closeQuietly(li);
}
FileUtils.deleteQuietly(input);
FileUtils.moveFile(tempFile, input);
}
I think what you want is random access. Check out the related java tutorial. However, I don't believe you can just insert data at an arbitrary point in the file; If I recall correctly, you'd only overwrite the data. If you wanted to insert, you'd have to have your code
copy a block,
overwrite with your new stuff,
copy the next block,
overwrite with the previously copied block,
return to 3 until no more blocks
As #atk suggested, java.nio.channels.SeekableByteChannel is a good interface. But it is available from 1.7 only.
Update : If you have no issue using FileUtils then use
String fileString = FileUtils.readFileToString(file);
This isn't a direct answer to the question, but often files are accessed via InputStreams. If this is your use case, then you can chain input streams via SequenceInputStream to achieve the same result. E.g.
InputStream inputStream = new SequenceInputStream(new ByteArrayInputStream("my line\n".getBytes()), new FileInputStream(new File("myfile.txt")));
I will leave it here just in case anyone need
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (FileInputStream fileInputStream1 = new FileInputStream(fileName1);
FileInputStream fileInputStream2 = new FileInputStream(fileName2)) {
while (fileInputStream2.available() > 0) {
byteArrayOutputStream.write(fileInputStream2.read());
}
while (fileInputStream1.available() > 0) {
byteArrayOutputStream.write(fileInputStream1.read());
}
}
try (FileOutputStream fileOutputStream = new FileOutputStream(fileName1)) {
byteArrayOutputStream.writeTo(fileOutputStream);
}