java向python ,text文件动态传参或传值问题完美解决

由于业务需要对python文件进行参数传递,通过下面两个java方法完美解决此问题,我的思路是:首先我要先把上次写的参数删除,第二我要新的参数写到python文件中。

第一个方法解决了删除上次传递的参数问题。

第二个方法解决了参数传递到python文件

/**
     * @param file python文件的路径
     * @return
     * @throws IOException
     */
    private List<String> readAndRemoveFirstLines(File file) throws IOException{     //删除第一行内容
        int lineNum=1;
        List<String> strList = new ArrayList<String>();
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(file, "rw");
            //Initial write position
            long writePosition = raf.getFilePointer();
            for (int i = 0; i < lineNum; i++) {
                String line = raf.readLine();
                if (line == null) {
                    break;
                }
                strList.add(line);
            }

            // Shift the next lines upwards.
            long readPosition = raf.getFilePointer();
            byte[] buff = new byte[1024];
            int n;
            while (-1 != (n = raf.read(buff))) {
                raf.seek(writePosition);
                raf.write(buff, 0, n);
                readPosition += n;
                writePosition += n;
                raf.seek(readPosition);

            }
            raf.setLength(writePosition);

        } catch(IOException e){
            throw e;
        } finally{
            try{
                if(raf != null){
                    raf.close();
                }
            }catch(IOException e){
                throw e;
            }
        }
        return strList;
    }
    /**
     * @param filePath  python文件的路径
     * @param contents  传入python文件的变量,仅数值即可
     * @throws IOException
     */
    public void addContainsToFile(String filePath, String contents) throws IOException {
        //1、参数校验
        int position =0;
        File file = new File(filePath);
        //定义python文件写入的内容
        String content="dis = " + contents + " Meters"+"\n";
        //判断文件是否存在
        if (!(file.exists() && file.isFile())) {
            System.out.println("文件不存在  ~ ");
            return;
        }
        //判断position是否合法
        if ((position < 0) || (position > file.length())) {
            System.out.println("position不合法 ~ ");
            return;
        }
        //2、创建临时文件
        File tempFile = File.createTempFile("sss", ".temp", new File("D:/"));
        //3、用文件输入流、文件输出流对文件进行操作
        FileOutputStream outputStream = new FileOutputStream(tempFile);
        FileInputStream inputStream = new FileInputStream(tempFile);
        //在退出JVM退出时自动删除
        tempFile.deleteOnExit();
        //4、创建RandomAccessFile流
        RandomAccessFile rw = new RandomAccessFile(file, "rw");
        //文件指定位置到 position
        rw.seek(position);
        int tmp;
        //5、将position位置后的内容写入临时文件
        while ((tmp = rw.read()) != -1) {
            outputStream.write(tmp);
        }
        //6、将追加内容 contents 写入 position 位置
        rw.seek(position);
        rw.write(content.getBytes());
        //7、将临时文件写回文件,并将创建的流关闭
        while ((tmp = inputStream.read()) != -1) {
            rw.write(tmp);
        }
        rw.close();
        outputStream.close();
        inputStream.close();
    }

测试方法我用的Junit进行测试:

  @Test
    public  void  testAddLine() throws IOException {
        try {
            //重点注意:1.以下三个方法的执行顺序不能改变;2.python文件文件的首行必须添加默认参数dis的数值;3.文件路径填写正确


            //此方法用来移除python文件中首行内容,在python文件的首行必须添加默认参数dis的数值,如:dis = 200 +'meter'
            readAndRemoveFirstLines(new File("E:\\demo\\pointbufferandpolygon.py"));//此方法用来给python文件首行添加dis的变量值,注意:变量名在此方法中已写死,如需修改变量名,参考此方法
            addContainsToFile("E:\\demo\\pointbufferandpolygon.py", "90000");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

效果图:

如果要传递多个参数呢?我这里已经实现了,也是通过以上代码进行了二次封装,如果需要可以留言进行探讨。

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0