我在Solaris 10上运行Java 1.5.我的程序是一个独立的java程序,使用java并发包和log4j-1.2.12.jar来记录某些信息.主要逻辑如下
ExecutorService executor = new AppThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(Integer.MAX_VALUE), new AppThreadFactory("BSRT", true), new ThreadPoolExecutor.CallerRunsPolicy());
CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(executor);
for (final Integer id : taskList) {
Callable<Integer> c = new Callable<Integer>() {
public Integer call() throws Exception {
int newId = DB operation(id);
return newId;
}
};
completionService.submit(c);
}
logger.debug("Start retrievie result");
for (Integer id : taskList) {
try {
Future<Integer> future = completionService.poll(1, TimeUnit.SECONDS);
Integer taskId=null;
if (future != null) {
logger.debug("future is obtained.");
taskId …Run Code Online (Sandbox Code Playgroud) 我正在使用API(Jasper Reports)生成PDF,我想在内存中完成大部分工作.我已经能够将生成的文件作为一个OutputStream,现在我只是在寻找一个实现器,用于将内存保存在内存中InputStream.
我可以使用哪些类,既可以作为OutputStream和InputStream在内存中的数据?
我试图模拟非对称密钥系统.我使用以下代码生成密钥对,加密,解密密码.我有一个分布式环境,目前我保存在文件系统中生成的密钥.我知道这不安全,但仅用于测试目的.
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,
NoSuchPaddingException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(256, random);
KeyPair pair = generator.generateKeyPair();
Key pubKey = pair.getPublic();
Key privKey = pair.getPrivate();
//store public key
try {
storeKey(pubKey, Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat("-publickey")));
} catch (Exception e) {
e.printStackTrace();
DBLogger.logMessage(e.toString(), Status.KEY_GENERATION_ERROR);
}
//store private key
try {
storeKey(privKey, Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat("-privatekey")));
} catch (Exception e) {
e.printStackTrace();
DBLogger.logMessage(e.toString(), Status.KEY_GENERATION_ERROR);
}
}
protected synchronized String …Run Code Online (Sandbox Code Playgroud) 我无法在下面的代码中解决以下异常.我使用BufferedReader的方式有什么问题?我在main方法中使用BufferedReader
输出: -
ParseFileName.java:56: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
Run Code Online (Sandbox Code Playgroud)
// ParseFileName is used to get the file name from a file path
// For eg: get - crc.v from "$ROOT/rtl/..path/crc.v"
import java.util.regex.Pattern;
import java.io.*;
public class ParseFileName {
//Split along /'s , and collect the last term.
public String getName (String longName) {
String splitAt = "/";
Pattern pattern1 = Pattern.compile(splitAt);
String[] parts = pattern1.split(longName);
System.out.println("\nparts.length = …Run Code Online (Sandbox Code Playgroud) 如何读取Stringwith DataInputStream,与此代码一起存储:
DataOutputStream dataOut = new DataOutputStream (out); // Some other stream
String title = processed.getTitle();
dataOut.writeInt(title.length());
dataOut.writeBytes(title);
Run Code Online (Sandbox Code Playgroud) 我有一个包含多个文件的目录。我只需要使用 Java 检索列表中的 XML 文件名。我怎样才能做到这一点?
我正在使用一个需要Reader读取的API,这Reader实际上应该从一个(可能非常大)读取StringBuilder.
但是使用这个:
new StringReader(stringBuilder.toString());
Run Code Online (Sandbox Code Playgroud)
...将复制内部StringBuilder的char数组,由于数组大小,我想避免使用它.虽然,这个char数组是受包保护的.
并没有更好的运气StringBuffer:(
我错过了什么吗?
注意:我目前无法使用Java 7.
我有一个文本文件,其中包含我的计算机上保存的内容abcdefgh.我想使用FileInputStream来显示控制台上的字符,同时还要测量执行此操作所需的时间.它看起来像这样:
public class Readtime {
public static void main(String args[]) throws Exception{
FileInputStream in=new FileInputStream("bolbol.txt");
while(in.read()!=-1){
long startTime = System.nanoTime();
int x = in.read();
long endtime = System.nanoTime();
System.out.println(endtime-startTime);
System.out.println((char)x);
}
in.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我在控制台上得到的是以下内容:
8863
b
7464
d
6998
f
6997
h
Run Code Online (Sandbox Code Playgroud)
其余的字母现在在哪里?就好像只进行了4次读操作一样.我的想法是朝着字符大小的方向前进,一次read()只能读取一个字节,但我没有到达任何地方.
我正在尝试为我重构的方法编写一个Junit测试,以确保该方法仍然按预期运行.我注意到一些我无法弄清楚的奇怪行为.
为什么Java ByteArrayOutputStream.toByteArray()会返回一些随机字节?我想这可能与记忆有关.数组中的11个字节始终是随机的.任何见解?
这是我重构的方法.
package foo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static byte[] createZip(Map<String, byte[]> files) throws IOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ZipOutputStream zipfile = new ZipOutputStream(bos);
String fileName = null;
ZipEntry zipentry = null;
for (Map.Entry<String, byte[]> entry : files.entrySet()) {
fileName = entry.getKey();
zipentry = new ZipEntry(fileName);
zipfile.putNextEntry(zipentry);
zipfile.write(files.get(fileName));
}
zipfile.close();
return bos.toByteArray();
}
}
Run Code Online (Sandbox Code Playgroud)
测试类
package foo;
import …Run Code Online (Sandbox Code Playgroud) java ×10
java-io ×10
file ×2
file-io ×2
bytearray ×1
directory ×1
encryption ×1
filereader ×1
io ×1
ioexception ×1
log4j ×1
rsa ×1
zip ×1