java-io

Zip file created on server and download that zip, using java

感情迁移 提交于 2019-12-04 12:55:41
I have the below code got from mkyong, to zip files on local. But, my requirement is to zip files on server and need to download that. Could any one help. code wrote to zipFiles: public void zipFiles(File contentFile, File navFile) { byte[] buffer = new byte[1024]; try{ // i dont have idea on what to give here in fileoutputstream FileOutputStream fos = new FileOutputStream("C:\\MyFile.zip"); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze= new ZipEntry(contentFile.toString()); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(contentFile.toString()); int len; while (

to edit a specific line in a textfile using java program

强颜欢笑 提交于 2019-12-04 12:47:21
Ok, say I have a text file called "people.txt", and it contains the following information: 1 adam 20 M 2 betty 49 F 3 charles 9 M 4 david 22 M 5 ethan 41 M 6 faith 23 F 7 greg 22 M 8 heidi 63 F Basically, the first number is the ID of the person, then comes the person's name, age and gender. Say I want to replace line 2, or the person with ID number 2 with different values. Now, I know I cant use RandomAccessFile for this because the names are not always the same number of bytes, neither are the ages. While searching random Java forums, I found that StringBuilder or StringBuffer should suffice

InputStream, mark(), reset()

强颜欢笑 提交于 2019-12-04 10:28:30
问题 How are mark() and reset() methods working exactly(in code below), step by step ? I tried to write my own example but is starts to throw wrong mark exception or similar to that, and I cannot understand what is the point of placing mark and reset methods in this code because I don't see difference with this or without. import java.io.*; class BufferedInputStreamDemo { public static void main(String args[]) { String s = "© is a copyright symbol, " + "however &copy isn't.\n"; byte buf[] = s

Why is File.exists() behaving flakily in multithreaded environment?

我是研究僧i 提交于 2019-12-04 10:19:14
I have a batch process running under java JDK 1.7. It is running on a system with RHEL, 2.6.18-308.el5 #1 SMP. This process gets a list of metadata objects from a database. From this metadata it extracts a path to a file. This file may or may not actually exist. The process uses the ExecutorService ( Executors.newFixedThreadPool() ) to launch multiple threads. Each thread runs a Callable that launches a process that reads that file and writes another file if that input file exists (and logs the result) and does nothing if the file does not exist (except log that result). I find the behavior is

Apache hive error Merging of credentials not supported in this version of hadoop

丶灬走出姿态 提交于 2019-12-04 07:33:52
I am using hadoop 1.2.1, hbase 0.94.14 and hive 1.0.0. There are three datanodes in my clsuter and three regionservers also. I have to import some data from hbase to hive. I have configured hive successfully but when I ran a command to count no. of rows in hive table, its gives following ERROR [main]: exec.Task (SessionState.java:printError(833)) - Job Submission failed with exception 'java.lang.RuntimeException(java.io.IOException: Merging of credentials not supported in this version of hadoop)' java.lang.RuntimeException: java.io.IOException: Merging of credentials not supported in this

What is the difference between getResourceAsStream(“Words.txt”) and FileInputStream(“./src/package/Words.txt”)? [duplicate]

那年仲夏 提交于 2019-12-04 06:58:30
问题 This question already has answers here : getResourceAsStream() vs FileInputStream (6 answers) Closed 3 years ago . I am currently writing a servlet based application (the client side). I tried to get a text file inside the same package where the code is located. All of the methods that I have come across used either MyClass.class.getResourceAsStream("Words.txt") or classLoader.getResourceAsStream("Words.txt") to get the text file (eg: SO1, SO2). But I have tried FileInputStream("./src/package

How to use javas Process.waitFor()?

被刻印的时光 ゝ 提交于 2019-12-04 06:00:35
问题 I am trying to run command line commands from Java and a quick sanity check made me realize that the reason I am having trouble is that I can't get the pr.waitFor() call below to work. This programs ends in less than 30s and prints nothing after "foo:". I expected it to take just over 30 s and print a random number after "foo:". What am I doing wrong? import java.io.BufferedReader; import java.io.InputStreamReader; public class Sample { public static void main(String[] args) throws Exception

java : writing large files?

放肆的年华 提交于 2019-12-04 05:24:12
Greetings , I get huge number of records from database and write into a file.I was wondering what the best way to write huge files. (1Gb - 10Gb). Currently I am using BufferedWriter BufferedWriter mbrWriter=new BufferedWriter(new FileWriter(memberCSV)); while(done){ //do writings } mbrWriter.close(); If you really insist using Java for this, then the best way would be to write immediately as soon as the data comes in and thus not to collect all the data from ResultSet into Java's memory first. You would need at least that much of free memory in Java otherwise. Thus, do e.g. while (resultSet

The FileReader cannot find my text file

。_饼干妹妹 提交于 2019-12-03 21:38:38
I have a simple text file and for some reason, it cannot be found. I don't see anything wrong with the code because I got it from a site, and I am starting to think that I didn't place the text file in the right place. Any suggestion please? Code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.nio.file.Path; import java.nio.file.Paths; public class MainFavorites { public static void main(String[] args) throws IOException { /** * finds pathway to the file */ // File file = new File(

javax.crypto.BadPaddingException: unknown block type

旧时模样 提交于 2019-12-03 21:32:34
I am trying to simulate asymmetric key system. I use following code to generate key pairs, encrypt, decrypt passwords. I have a distributed environment and for the moment I save the keys generated in a file system. I know that is not secure but its just for testing purposes. private static SecureRandom random = new SecureRandom(); static { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); } protected synchronized void generateKeys() throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException,