File oldFile = new File("old");
if (oldFile.renameTo(new File("new"))){
System.out.println(oldFile.getName());//this prints "old"
}
Run Code Online (Sandbox Code Playgroud)
我查看了openJDK源代码,并且renameTo(File dest)函数如下所示:
public class File implements Serializable, Comparable<File> {
static private FileSystem fs = FileSystem.getFileSystem();
private String path;
...
public boolean renameTo(File dest) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
security.checkWrite(dest.path);
}
return fs.rename(this, dest);
}
...
}
Run Code Online (Sandbox Code Playgroud)
所以路径变量永远不会改变.为什么会这样?使用重命名的File变量的正确方法是什么?目前我这样做:
File oldFile = new File("/home/blin/misk/old");
File newFile = new File("/home/blin/misk/new");
if (oldFile.renameTo(newFile)){
oldFile=newFile;
System.out.println(oldFile.getName());//this prints "new"
}
Run Code Online (Sandbox Code Playgroud) 我需要我的程序来创建和编辑配置文件,配置文件包含有关对象集的信息,而不是在每次执行时读取它.我可以使用配置样式的某种指导方针吗?
我在Windows上使用C++.
在使用标准库时,我发现python2和python3之间存在一些奇怪的区别.如果我尝试在python2中捕获信号,而TCPServer在不同的线程中运行,则信号不会被处理,但在python3中它确实如此.
这是一个重现问题的脚本
import signal
import threading
import sys
if sys.version_info > (3,0):
from socketserver import TCPServer, BaseRequestHandler
else:
from SocketServer import TCPServer, BaseRequestHandler
def shutdown(signum, frame):
print("Shutting down server thread")
server.shutdown()
server = TCPServer(
('127.0.0.1', 7654),
BaseRequestHandler
)
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
server_thread = threading.Thread(target=server.serve_forever)
print("Starting server thread")
server_thread.start()
print("Waiting for server thread to shut down")
server_thread.join()
print("Server thread terminated")
Run Code Online (Sandbox Code Playgroud)
这是python3的输出:
Starting server thread
Waiting for server thread to shut down
^CShutting down server thread
Server thread terminated
Run Code Online (Sandbox Code Playgroud)
这是来自python2:
Starting …Run Code Online (Sandbox Code Playgroud) 我有几个应用程序在指定的时间间隔内运行.为了监控OutOfMemoryError,我决定启用HeapDumpOnOutOfMemoryError,在此之前,我决定做一些研究.某些应用程序的最大堆大小为2GB,因此快速连续生成多个堆转储会占用所有磁盘空间.
我写了一个小脚本来检查它是如何工作的.
import java.util.LinkedList;
import java.util.List;
public class Test implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(new Test()).start();
}
public void run() {
while (true) {
try{
List<Object> list = new LinkedList<Object>();
while (true){
list.add(new Object());
}
}
catch (Throwable e){
System.out.println(e);
}
try {
Thread.sleep(1000);
}
catch (InterruptedException ignored) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果
$ java -XX:+HeapDumpOnOutOfMemoryError -Xmx2M Test
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid25711.hprof ...
Heap dump file created [14694890 bytes …Run Code Online (Sandbox Code Playgroud) 我的一个类在列表中累积值,使用该列表作为另一个对象上的方法的参数,并删除此列表中的一些值。就像是
element = element_source.get()
self.elements.append(element)
element_destination.send(elements)
self.remove_outdated_elements()
Run Code Online (Sandbox Code Playgroud)
但是当我试图测试这种行为时,我发现模拟不会复制它们的参数。
>>> from unittest.mock import Mock
>>> m = Mock()
>>> a = [1]
>>> m(a)
<Mock name='mock()' id='139717658759824'>
>>> m.call_args
call([1])
>>> a.pop()
1
>>> m.assert_called_once_with([1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/unittest/mock.py", line 737, in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
File "/usr/lib/python3.3/unittest/mock.py", line 726, in assert_called_with
raise AssertionError(msg)
AssertionError: Expected call: mock([1])
Actual call: mock([])
Run Code Online (Sandbox Code Playgroud)
有没有办法让 Mock 复制它的调用参数?如果不是,那么测试这种行为的最佳方法是什么?
我有
no matching function for call to 'saveLine::saveLine()'
Run Code Online (Sandbox Code Playgroud)
编译我的应用程序时出错.实际上从未调用过construcor.
saveLine类定义:
class saveLine
{
public:
saveLine(QWidget *parent);
private:
QPushButton *selectButton, *acceptButton;
QLabel *filePath;
QLineEdit *allias;
};
Run Code Online (Sandbox Code Playgroud)
saveLine用于另一个类,定义如下:
class MWindow : public QWidget
{
Q_OBJECT
public:
MWindow(QWidget *parent=0);
private:
saveLine line1;
};
Run Code Online (Sandbox Code Playgroud)
错误指向MWindow构造函数实现
MWindow::MWindow(QWidget *parent):QWidget(parent)
{
this->setWindowTitle("Launcher");
this->resize(600,600);
}
Run Code Online (Sandbox Code Playgroud)
我该怎么办?我打算在向量中使用saveLine类,以在运行时创建行.
编辑:我错误地宣布了line1,它应该读
saveLine *line1;
Run Code Online (Sandbox Code Playgroud)
但现在又出现了另一个错误
ISO C++ forbids declaration of 'saveLine' with no type
Run Code Online (Sandbox Code Playgroud)
和
expected ';' before '*' token
Run Code Online (Sandbox Code Playgroud)
在这条线上.似乎saveLine不再被认为是一个类,怎么会这样?