我正在尝试OutputStream将Process启动者exec()发送到控制台.如何才能做到这一点?
这是一些不完整的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
public class RuntimeTests
{
public static void main(String[] args)
{
File path = new File("C:\\Dir\\Dir2");
String command = "cmd /c dir";
Reader rdr = null;
PrintStream prtStrm = System.out;
try
{
Runtime terminal = Runtime.getRuntime();
OutputStream rtm = terminal.exec(command, null, path).getOutputStream();
prtStrm = new PrintStream(rtm);
prtStrm.println();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) 有谁知道如何将数字四舍五入到最接近的5的倍数?我发现了一个算法将它四舍五入到最近的10的倍数,但我找不到这个.
这样做十分.
double number = Math.round((len + 5)/ 10.0) * 10.0;
Run Code Online (Sandbox Code Playgroud) 我试图删除一个文件,我有两个,一个稍微改变,所以我可以删除旧文件,并用我更改的新文件替换它.当我尝试删除该文件时,我收到错误消息"正在使用的文件",其中表示该操作无法完成,因为该文件是在Java(TM)Platform SE二进制文件中打开的.
我怎么关闭它?
我正在将CSV文件导入MySQL数据库.这可以通过java.mysql支持文件路径中的正斜杠来完成.如果用户提供路径
c:\upload\date\csv\sample.csv
Run Code Online (Sandbox Code Playgroud)
MySQL不支持这种类型的路径模式.我想在路径中搜索反斜杠并用正斜杠替换它们,以便:
c:/upload/date/csv/sample.csv
Run Code Online (Sandbox Code Playgroud)
怎么做的?
我正在尝试将.MID文件读入Java程序,并希望将每个音符/和弦分开,以便在某种UI上显示它们.我在Java中使用Sequencer API并没有太多运气,并且尝试直接使用MidiFileReader对我来说也不起作用.我会附上我在这里使用的代码,如果有人想看到的话:
package miditest;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException{
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));
// Create a sequencer for the sequence
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
// Start playing
sequencer.start();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要在Java中进行一些浮点运算,如下面的代码所示:
public class TestMain {
private static Map<Integer, Double> ccc = new HashMap<Integer, Double>() {
{ put(1, 0.01); put(2, 0.02); put(3, 0.05); put(4, 0.1); put(6, 0.2);
put(10, 0.5); put(20, 1.0); put(30, 2.0); put(50, 5.0); put(100, 10.0);
}
};
Double increment(Double i, boolean up) {
Double inc = null;
while (inc == null) {
inc = ccc.get(i.intValue());
if (up)
--i;
else
++i;
}
return inc;
}
public static void main(String[] args) {
TestMain tt = new TestMain();
for (double i = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来填充一个带有名称的国家列表的微调器.我可以从Android操作系统中检索它吗?有人可以请我举个例子吗?
我试图将我的联系人保存在我的表中但是filechosser总是设置为所有文件.有没有办法我可以设置它只接受.txt并使其成为默认或唯一选项.
savecontact.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filesave = new JFileChooser();
int returnVal = filesave.showSaveDialog(Main.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File file = filesave.getSelectedFile();
PrintWriter os = new PrintWriter(file);
os.println("");
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col) + "\t");
}
os.println("");
os.println("");
for (int row = 0; row < table.getRowCount(); row++) {
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col));
os.print(": ");
os.println(table.getValueAt(row, col));
}
os.println("");
}
os.close(); …Run Code Online (Sandbox Code Playgroud) 在运行相同Java应用程序的同一网络中连接的两台可分离计算机如何通过在彼此之间同步堆来维持相同状态的基本原理是什么?
我相信Terracotta完成了这项任务,但我不知道一些伪代码会如何描述其核心功能.
我只是在寻找对这项技术的理解.
你好,我有一些关于java的问题.这是我的代码:
public static void main(String[] args) throws Exception {
Process pr = Runtime.getRuntime().exec("java -version");
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
pr.waitFor();
System.out.println("ok!");
in.close();
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
在那个代码我试图得到一个java版本命令执行是好的,但我无法读取输出它只是返回null.为什么?
java ×10
file-io ×2
runtime ×2
runtime.exec ×2
android ×1
audio ×1
countries ×1
file ×1
file-locking ×1
filehandle ×1
heap ×1
javasound ×1
jfilechooser ×1
jvm ×1
locale ×1
midi ×1
printstream ×1
regex ×1
replication ×1
rounding ×1
stream ×1
swing ×1