简体   繁体   English

Java - 文件读/写

[英]Java - File Reading/Writing

I got some java files in my workspace. 我的工作区里有一些java文件。 Now i want to write a java program which would read a text file coming from a different source, one at a time, line by line and insert those lines into respective java files in the workspace . 现在我想编写一个java程序,它将读取来自不同源的文本文件,一次一个,逐行,并将这些行插入工作区中的相应java文件。

The text file would say me which file to insert into which ".java" class. 文本文件会告诉我要插入哪个“.java”类的文件。

For example I have AddressType.Java in my workspace with getters, setters 例如,我的工作区中有AddressType.Java,包含getter,setter

I got a text file namely AddressType.txt which has some additional methods, comments, instance variables etc etc which should actually go and sit into the body of the AddressType.Java file. 我有一个文本文件,即AddressType.txt,它有一些额外的方法,注释,实例变量等等,它们实际应该放在AddressType.Java文件的主体中。

For this i have a logic in my mind and its sort of working. 为此我在脑海中有一个逻辑和它的工作。 I know how to read/write to files. 我知道如何读/写文件。 Here is my problem. 这是我的问题。 My approach is to replace the ending bracket } in AddressType.Java with a space and then pull the contents of AddressType.txt into AddressType.java and then close the } again. 我的方法是使用空格替换AddressType.Java中的结束括号},然后将AddressType.txt的内容拉入AddressType.java,然后再次关闭}。

Can some one help me how to find EOF } in a .java file using a java program. 有人可以帮助我如何使用java程序在.java文件中找到EOF}。

I cannot do this manually as I have 100's of java files and text files to move content to. 我无法手动执行此操作,因为我有100个java文件和文本文件来移动内容。

Please help. 请帮忙。 I hope some one understands this question. 我希望有人能理解这个问题。

Nothing is run time. 没有什么是运行时间。 Everything is compile time itself. 一切都是编译时间本身。 I mean i need to merge the txt file contents into java file and then compile and ship the java files. 我的意思是我需要将txt文件内容合并到java文件中,然后编译并发送java文件。

You can read the entire java text file into memory and remove the last } that way. 您可以将整个Java文本文件读入内存,然后以这种方式删除最后一个}。 Classes can't be very big. 班级不能太大。 ie less than a few MB. 即少于几个MB。 And you can do this with IOUtils.toString() in one line. 您可以在一行中使用IOUtils.toString()进行此操作。

String textWithoutLast = IOUtils.toString(
      new FileInputStream(javaFilename)
      ).replaceAll("}\\s*$","");

This removes the last } with any amount of spaces, tabs or new lines after it. 这将删除最后一个},并在其后加上任意数量的空格,制表符或换行符。

你必须计算开口{ s和关闭}一旦你有相同的数字你找到了结束的...

熟悉编写Eclipse插件的人可能能够扩展现有的代码格式化程序插件来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM