修改文件内容

vi命令在linux下再熟悉不过了,搭配unzip和zip还可以修改压缩包里的文件。

ps: 如果本机还没有安装zip,unzip,先执行安装命令

sudo apt-get install unzip

sudo apt-get install zip

假设现在有如下结构的目录:

HelloWorld/

└── src

└── main

├── META-INF

│   └── MANIFEST.MF

├── java

│   └── com

│   └── thxopen

│   └── demo

│   └── Main.java

└── resources

└── demo.txt

Main.java 文件内容如下:

package com.thxopen.demo;

import java.io.BufferedInputStream;

import java.io.IOException;

import java.io.InputStream;

/**

* @author Keith Shan

* @date 2019年5月7日

* @site http://www.thxopen.com

*/

public class Main {

public static void main(String[] args) {

System.out.println("Hello World!");

InputStream uri = Main.class.getClassLoader().getResourceAsStream("demo.txt");

BufferedInputStream bf = null;

try {

bf = new BufferedInputStream(uri);

byte[] b = new byte[bf.available()];

bf.read(b);

System.out.println(new String(b));

} catch (IOException e) {

e.printStackTrace();

} finally {

if (bf != null) {

try {

bf.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

代码很简单,输出一个Hello World! 然后读取resources/demo.txt文件内容并输出。

我们把项目构建成jar上传到服务器,并在服务器上执行。下面是执行输出的结果

thxopen@PC201503302026:~/work$java -jar HelloWorld.jar

Hello World!

I am Keith Shan.

现在需要修改demo.txt的内容,改变输出,不需要重新打包上传,使用vi命令即可

thxopen@PC201503302026:~/work$vi HelloWorld.jar

" zip.vim version v28

" Browsing zipfile /home/thxopen/work/HelloWorld.jar

" Select a file with cursor and press ENTER

META-INF/MANIFEST.MF

com/

com/thxopen/

com/thxopen/demo/

com/thxopen/demo/Main.class

demo.txt

META-INF/

META-INF/HelloWorld.kotlin_module

然后会列出jar里面的内容,把光标移动到需要编辑的文件,回车即可编辑。

ps: 如果文件比较多,可以使用/filename 来查找定位

进入文件编辑状态后,操作就和vi命令一样,这里我新增I am the new String. 内容,:wq!保存退出vi,然后:exit退出jar编辑状态。

重新执行

thxopen@PC201503302026:~/work$java -jar HelloWorld.jar

Hello World!

I am Keith Shan.

I am the new String.

从输出可以看到demo.txt内容被成功修改

替换文件

修改文件内容对于.class文件来说似乎就不行了,那只能直接替换文件,如果替换文件呢?

我修改Main.java文件,修改后内容如下:

package com.thxopen.demo;

/**

* @author Keith Shan

* @date 2019年5月7日

* @site http://www.thxopen.com

*/

public class Main {

public static void main(String[] args) {

System.out.println("Class file modified");

}

}

我把编译好的class文件放到 /com/thxopen/demo目录下,如下所示

thxopen@PC201503302026:~/work$tree

.

├── HelloWorld.jar

└── com

└── thxopen

└── demo

└── Main.class (重新编译好的文件)

3 directories, 2 files

执行如下命令把文件替换到jar中

thxopen@PC201503302026:~/work$jar -uvf HelloWorld.jar com/thxopen/demo/Main.class

adding: com/thxopen/demo/Main.class(in = 557) (out= 351)(deflated 36%)

重新执行,class文件已经被替换

thxopen@PC201503302026:~/work$java -jar HelloWorld.jar

Class file modified

附:jar命令的基本用法

thxopen@PC201503302026:~/work$jar

Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...

Options:

-c create new archive

-t list table of contents forarchive

-x extract named (or all) files from archive

-u update existing archive 更新存在的压缩包

-v generate verbose output on standard output 生成日志到标准输出

-f specify archive file name 指定压缩包名称

-m include manifest information from specified manifest file

-n perform Pack200 normalization after creating a new archive

-e specify application entry point forstand-alone application

bundled into an executable jar file

-0 store only; use no ZIP compression

-P preserve leading '/' (absolute path) and ".." (parent directory) components from file names

-M donot create a manifest file forthe entries

-i generate index information forthe specified jar files

-C change to the specified directory and include the following file

If any file is a directory thenit is processed recursively.

The manifest file name, the archive file name and the entry point name are

specified inthe same order as the 'm', 'f' and 'e' flags.

Example 1: to archive two class files into an archive called classes.jar:

jar cvf classes.jar Foo.class Bar.class

Example 2: use an existing manifest file 'mymanifest' and archive all the

files inthe foo/ directory into 'classes.jar':

jar cvfm classes.jar mymanifest -C foo/ .

linux ps被替换文件,在linux下从jar中替换、修改文件相关推荐

  1. linux 修改文件内容sed,linux下通过sed命令直接修改文件内容

    linux下通过sed命令直接修改文件内容 sed是实现对流的编辑.通常,我们使用sed可以实现内容的编辑后然后保存成另外的一个文件,如果正确的话,才写入到源文件.但是某些时候,我们需要直接修改文件, ...

  2. Linux下shel脚本之批量修改文件扩展名

    Linux下shel脚本之批量修改文件扩展名 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 1.在/data/tmp/下的所有文件扩展名改为.py ...

  3. linux依赖包在哪个目录,命令-Linux cmd在jar中搜索类文件,而与jar路径无关

    Linux,演练以在许多jar中查找类文件. 转到包含下面的jar的目录. eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ ls blah.txt metric ...

  4. java中打开文件显示_从java程序中打开任何文件

    在 java中打开文件似乎有点棘手 – 对于.txt文件,必须将File对象与Scanner或BufferedReader对象结合使用 – 对于图像IO,必须使用 ImageIcon类 – 如果要打开 ...

  5. python导入文件夹数据有改动_python办公自动化--批量修改文件/文件夹名称

    导语 今天我们来看下如何批量修改名称.这个需求在工作中比较常见的,日常生活中可能也有此类需求,比如,打包下载了一部连续剧或有声读物,每个文件名却被加上了网址.网站名称,还有一些莫名其妙的符号,整得特别 ...

  6. freemarker 从 spring boot execute jar可执行jar中访问模板文件

    2019独角兽企业重金招聘Python工程师标准>>> private static Configuration freemarkerCfg = null;static {freem ...

  7. linux替换jar包目录,Linux下用jar命令替换war包中的文件【转】

    问题背景:在Linux环境上的weblogic发布war包,有时候只是修改了几个文件,也要上传整个war包,这样很费时间,因此整理了一下Linux环境,更新单个文件的方法. 1.如果要替换的文件直接在 ...

  8. linux替换jar的文件,Linux下用jar命令替换war包中的文件【转】

    问题背景:在Linux环境上的weblogic发布war包,有时候只是修改了几个文件,也要上传整个war包,这样很费时间,因此整理了一下Linux环境,更新单个文件的方法. 1.如果要替换的文件直接在 ...

  9. linux中vi修改文件内容,linux VI模式下批量修改文件内容

    1.         :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky 2.         :n,$ ...

最新文章

  1. oracle如何复制表的索引,Oracle表与索引管理
  2. 机器学习实战3--豆瓣读书简介
  3. 【SSM】基于注解@Controller的控制器
  4. SpringBoot开发常用技术整合 代码上传至github上面去
  5. 最长上升子序列 java_最长上升子序列 O(nlogn)解法 (java)
  6. webx学习(二)——Webx Framework
  7. 调试程序Bug-陈棚
  8. 最大公约数及最小公倍数计算
  9. Xmodem Ymodem Zmodem 协议
  10. cloudstack vpc network egress-ingress rules
  11. 万能险生存金什么意思,一文告诉你!
  12. 深度体验特斯拉新Model S:游戏体验翻车,方向盘让人又爱又恨
  13. 你好,女神节有个给你的惊喜请查收
  14. CloudSim Plus任务调度策略对比
  15. idea 出现中文乱码
  16. epoll使用详解:epoll_create、epoll_ctl、epoll_wait、close
  17. 关于U盘变成RAW格式 windows无法格式化的解决方法
  18. 一文了解蛋白功能结构域预测与分析
  19. 如何写3DMAX的插件
  20. Android手机4G网络设置ipv6

热门文章

  1. n边形对角线交点问题
  2. 赵雅智:android教学大纲
  3. python中简述对象和类的关系_Python笔记-习题42 对象、类及从属关系
  4. 当当海航互相选择的背后:或是一个双赢局
  5. (一)微信小程序支付前后台
  6. Windows简体系统和繁体系统下的乱码处理
  7. PHP正则表达式提取html超链接中的h…
  8. 突击蓝桥杯嵌入式(一)——内容概览
  9. 自媒体人如何在千氪实现月收入过万?
  10. Eclipse Maven clean后错误: 找不到或无法加载主类com.xxx.ShopApplication