linux中file获取项目文件失败,需要用流的方式获取

/**字典配置文件*/
    public static Dictionaries DICTIONARIES = null;

    //设置新的配置文件地址
    private static String PATH = "dictionary.xml";

    public static void init() throws IOException {
        Resource resource = new ClassPathResource(PATH);
        String xmlStr = ProFileUtil.get(resource.getInputStream());
        JAXBContext context;
        try {
            context = JAXBContext.newInstance(Dictionaries.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            DICTIONARIES = (Dictionaries) unmarshaller.unmarshal(new StringReader(xmlStr));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
通过流的方式获取地址linux中不会报错
要在Linux下使用Qt获取界面设定路径,并将test.bin保存在指定路径,你可以按照以下步骤操作: 1. 在你的Qt项目,添加一个QPushButton控件,用于触发保存操作。例如,将该按钮命名为saveButton。 2. 在你的界面类的头文件添加一个槽函数,用于处理保存按钮的点击事件。例如,可以在名为MainWindow的类添加一个名为onSaveButtonClicked的槽函数: ```cpp class MainWindow : public QMainWindow { Q_OBJECT public slots: void onSaveButtonClicked(); }; ``` 3. 在界面类的实现文件实现onSaveButtonClicked槽函数,并在其获取界面设定路径并保存文件: ```cpp void MainWindow::onSaveButtonClicked() { QString filePath = QFileDialog::getSaveFileName(this, "Save File", QDir::homePath(), "Binary Files (*.bin)"); if (!filePath.isEmpty()) { QFile file(filePath); if (file.open(QIODevice::WriteOnly)) { // 在这里写入文件内容,这里只是示例 QByteArray data; data.append("Test data"); file.write(data); file.close(); } else { // 文件打开失败处理逻辑 } } } ``` 4. 在界面类的构造函数连接保存按钮的点击信号与槽函数: ```cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { // 创建保存按钮 QPushButton *saveButton = new QPushButton("Save", this); // 连接点击信号与槽函数 connect(saveButton, &QPushButton::clicked, this, &MainWindow::onSaveButtonClicked); } ``` 这样,当点击保存按钮时,会弹出文件保存对话框,选择路径后,将test.bin保存在指定路径。 希望这个例子对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值