java to python, text files or dynamic parameter passing by value problem perfect solution

As business needs to python file parameter passing, a perfect solution to this problem through the following two java method, my idea is: First, I have to first delete the last written argument, the second argument I want to write the new file in python .

The first solution to the problem delete the last parameter passed.

The second way to solve the parameters passed to the python file

  

    /**
     * @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(); } } The catch (IOException E) { the throw E; } } return strlist; } / ** * @param filePath python file path * @param Contents variable python incoming file, the value can only * @throws IOException * / public void addContainsToFile (filePath String, String Contents) throws IOException { // . 1, the parameter calibration int position = 0 ; File File = new new File (filePath); //Python content definition files are written Content = String "DIS =" + + Contents "Meters" + "\ n-" ; // determine whether the file exists IF ((File.Exists () &&! File.isFile ())) { System.out.println ( " file does not exist ~ " ); return ; } // determining whether legitimate position iF ((position <0) || (position> file.length ())) { System.out.println ( " illegal ~ position " ); return ; } // 2, creates a temporary file file TEMPFILE = File.createTempFile ( "sss", ".temp", new new file ( "D: /")); //3, the input stream file, file output stream of documents to operate a FileOutputStream the outputStream = new new a FileOutputStream (TEMPFILE); the FileInputStream inputStream = new new the FileInputStream (TEMPFILE); // automatically deleted upon exiting the JVM exits tempFile.deleteOnExit (); // . 4 create RandomAccessFile flow RandomAccessFile rw = new new RandomAccessFile (file, "rw" ); // file specifies the location to position rw.seek (position); int tmp; // 5, the content of position after position to write temporary files the while ( ! (tmp = rw.read ()) = -1 ) { OutputStream.write (tmp); } // 6, the additional content contents written position location rw.seek (position); rw.write (content.getBytes ()); // 7, will be written back to temporary files, and the created flow off the while ((tmp = InputStream.read ()) = -1! ) { rw.write (tmp); } rw.close (); outputStream.close (); inputStream.close (); }

I use Junit test methods for testing:

  @Test
     public   void   testAddLine () throws IOException {
         the try {
             // important to note that: 1. the order of execution of the following three methods can not be changed; the first line 2.python file must be added the value of the default parameter dis; 3 Fill in the file path correct 


            // this method is used to remove the first line python file content, default parameters must add the value of the first line dis python file, such as: dis = + 200 is 'Meter' 
            readAndRemoveFirstLines ( new new file ( "E: \\ Demo pointbufferandpolygon.py \\ " )); // variable this method is used to add the first line of the file python dis values Note: in this method, the variable name has been written to die, to modify the variable name, refer to this method 
            addContainsToFile (" E: \\ \\ pointbufferandpolygon.py Demo "," 90000 " ); 
        } the catch (IOException E) { 
            e.printStackTrace ();
        } catch(Exception e) { 
            e.printStackTrace (); 
        } 
    }

Renderings:

If you want to pass multiple parameters? I have achieved here, also had a package through the above code, you can leave a message if you need to explore.

 

Guess you like

Origin www.cnblogs.com/chen-ya-ping/p/10939412.html