java 修改系统配置文件内容

java 修改系统配置文件内容,第1张

不要直接访问配置文件,在启动初始化时,把配置文件etc/sysctlconf文件读取到一个内存中的HashMap里面去,可以使用单例模式实现,所有的添加、修改、访问全在内存中的HashMap中进行。

启动系统时,加载配置文件到对象,去判断重复等等。

退出系统时,将对象,重新覆盖一下原有的配置文件

运行时所有的修改,全部针对内存中的对象 *** 作

1基本概念的理解

绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:

C:/xyz/testtxt 代表了testtxt文件的绝对路径。>

动态加载配置文件信息,你可以写一个以properties为扩展名的文件,里面存放key=value的,

读key,自然获取value了,java中有Properties类,专门读取这个文件的,你上述代码就是说加载那个文件获取流,然后再用我说的类 *** 作。

比如你的文件叫confproperties,那路径就写

thisgetClassgetRecourceAs Stream("/confproperties");

该配置文件放到源文件夹下(通常src),不论是web项目还是java项目

Java API读写HDFS

public class FSOptr {

/

@param args

/

public static void main(String[] args) throws Exception {

// TODO Auto-generated method stub

Configuration conf = new Configuration();

makeDir(conf);

rename(conf);

delete(conf);

}

// 创建文件目录

private static void makeDir(Configuration conf) throws Exception {

FileSystem fs = FileSystemget(conf);

Path dir = new Path("/user/hadoop/data/20140318");

boolean result = fsmkdirs(dir);// 创建文件夹

Systemoutprintln("make dir :" + result);

// 创建文件,并写入内容

Path dst = new Path("/user/hadoop/data/20140318/tmp");

byte[] buff = "hello,hadoop!"getBytes();

FSDataOutputStream outputStream = fscreate(dst);

outputStreamwrite(buff, 0, bufflength);

outputStreamclose();

FileStatus files[] = fslistStatus(dst);

for (FileStatus file : files) {

Systemoutprintln(filegetPath());

}

fsclose();

}

// 重命名文件

private static void rename(Configuration conf) throws Exception {

FileSystem fs = FileSystemget(conf);

Path oldName = new Path("/user/hadoop/data/20140318/1txt");

Path newName = new Path("/user/hadoop/data/20140318/2txt");

fsrename(oldName, newName);

FileStatus files[] = fslistStatus(new Path(

"/user/hadoop/data/20140318"));

for (FileStatus file : files) {

Systemoutprintln(filegetPath());

}

fsclose();

}

// 删除文件

@SuppressWarnings("deprecation")

private static void delete(Configuration conf) throws Exception {

FileSystem fs = FileSystemget(conf);

Path path = new Path("/user/hadoop/data/20140318");

if (fsisDirectory(path)) {

FileStatus files[] = fslistStatus(path);

for (FileStatus file : files) {

fsdelete(filegetPath());

}

} else {

fsdelete(path);

}

// 或者

fsdelete(path, true);

fsclose();

}

/

下载,将hdfs文件下载到本地磁盘

@param localSrc1

本地的文件地址,即文件的路径

@param hdfsSrc1

存放在hdfs的文件地址

/

public boolean sendFromHdfs(String hdfsSrc1, String localSrc1) {

Configuration conf = new Configuration();

FileSystem fs = null;

try {

fs = FileSystemget(URIcreate(hdfsSrc1), conf);

Path hdfs_path = new Path(hdfsSrc1);

Path local_path = new Path(localSrc1);

fscopyToLocalFile(hdfs_path, local_path);

return true;

} catch (IOException e) {

eprintStackTrace();

}

return false;

}

/

上传,将本地文件copy到hdfs系统中

@param localSrc

本地的文件地址,即文件的路径

@param hdfsSrc

存放在hdfs的文件地址

/

public boolean sendToHdfs1(String localSrc, String hdfsSrc) {

InputStream in;

try {

in = new BufferedInputStream(new FileInputStream(localSrc));

Configuration conf = new Configuration();// 得到配置对象

FileSystem fs; // 文件系统

try {

fs = FileSystemget(URIcreate(hdfsSrc), conf);

// 输出流,创建一个输出流

OutputStream out = fscreate(new Path(hdfsSrc),

new Progressable() {

// 重写progress方法

public void progress() {

// Systemoutprintln("上传完一个设定缓存区大小容量的文件!");

}

});

// 连接两个流,形成通道,使输入流向输出流传输数据,

IOUtilscopyBytes(in, out, 10240, true); // in为输入流对象,out为输出流对象,4096为缓冲区大小,true为上传后关闭流

return true;

} catch (IOException e) {

eprintStackTrace();

}

} catch (FileNotFoundException e) {

eprintStackTrace();

}

return false;

}

/

移动

@param old_st原来存放的路径

@param new_st移动到的路径

/

public boolean moveFileName(String old_st, String new_st) {

try {

// 下载到服务器本地

boolean down_flag = sendFromHdfs(old_st, "/home/hadoop/文档/temp");

Configuration conf = new Configuration();

FileSystem fs = null;

// 删除源文件

try {

fs = FileSystemget(URIcreate(old_st), conf);

Path hdfs_path = new Path(old_st);

fsdelete(hdfs_path);

} catch (IOException e) {

eprintStackTrace();

}

// 从服务器本地传到新路径

new_st = new_st + old_stsubstring(old_stlastIndexOf("/"));

boolean uplod_flag = sendToHdfs1("/home/hadoop/文档/temp", new_st);

if (down_flag && uplod_flag) {

return true;

}

} catch (Exception e) {

eprintStackTrace();

}

return false;

}

// copy本地文件到hdfs

private static void CopyFromLocalFile(Configuration conf) throws Exception {

FileSystem fs = FileSystemget(conf);

Path src = new Path("/home/hadoop/wordtxt");

Path dst = new Path("/user/hadoop/data/");

fscopyFromLocalFile(src, dst);

fsclose();

}

// 获取给定目录下的所有子目录以及子文件

private static void getAllChildFile(Configuration conf) throws Exception {

FileSystem fs = FileSystemget(conf);

Path path = new Path("/user/hadoop");

getFile(path, fs);

}

private static void getFile(Path path, FileSystem fs)throws Exception {

FileStatus[] fileStatus = fslistStatus(path);

for (int i = 0; i < fileStatuslength; i++) {

if (fileStatus[i]isDir()) {

Path p = new Path(fileStatus[i]getPath()toString());

getFile(p, fs);

} else {

Systemoutprintln(fileStatus[i]getPath()toString());

}

}

}

//判断文件是否存在

private static boolean isExist(Configuration conf,String path)throws Exception{

FileSystem fileSystem = FileSystemget(conf);

return fileSystemexists(new Path(path));

}

//获取hdfs集群所有主机结点数据

private static void getAllClusterNodeInfo(Configuration conf)throws Exception{

FileSystem fs = FileSystemget(conf);

DistributedFileSystem hdfs = (DistributedFileSystem)fs;

DatanodeInfo[] dataNodeStats = hdfsgetDataNodeStats();

String[] names = new String[dataNodeStatslength];

Systemoutprintln("list of all the nodes in HDFS cluster:"); //print info

for(int i=0; i < dataNodeStatslength; i++){

names[i] = dataNodeStats[i]getHostName();

Systemoutprintln(names[i]); //print info

}

}

//get the locations of a file in HDFS

private static void getFileLocation(Configuration conf)throws Exception{

FileSystem fs = FileSystemget(conf);

Path f = new Path("/user/cluster/dfstxt");

FileStatus filestatus = fsgetFileStatus(f);

BlockLocation[] blkLocations = fsgetFileBlockLocations(filestatus,0,filestatusgetLen());

int blkCount = blkLocationslength;

for(int i=0; i < blkCount; i++){

String[] hosts = blkLocations[i]getHosts();

//Do sth with the block hosts

Systemoutprintln(hosts);

}

}

//get HDFS file last modification time

private static void getModificationTime(Configuration conf)throws Exception{

FileSystem fs = FileSystemget(conf);

Path f = new Path("/user/cluster/dfstxt");

FileStatus filestatus = fsgetFileStatus(f);

long modificationTime = filestatusgetModificationTime(); // measured in milliseconds since the epoch

Date d = new Date(modificationTime);

Systemoutprintln(d);

}

}

要使用Java修改FreeSWITCH的配置文件,您可以使用Java IO类库中的FileWriter和BufferedWriter类来打开、读取和写入配置文件。以下是一个简单的示例代码:

java

Copy code

import javaio;

public class ModifyConfigFile {

public static void main(String[] args) {

try {

// 打开配置文件

File file = new File("/usr/local/freeswitch/conf/sip_profiles/externalxml");

BufferedReader reader = new BufferedReader(new FileReader(file));

// 读取配置文件

String line;

StringBuilder stringBuilder = new StringBuilder();

while ((line = readerreadLine()) != null) {

// 进行修改 *** 作

if (linecontains("<param name=\"ext-rtp-ip\"")) {

line = "<param name=\"ext-rtp-ip\" value=\"1921681100\"/>";

}

stringBuilderappend(line)append("\n");

}

readerclose();

// 写入修改后的内容

FileWriter writer = new FileWriter(file);

BufferedWriter bufferedWriter = new BufferedWriter(writer);

bufferedWriterwrite(stringBuildertoString());

bufferedWriterclose();

Systemoutprintln("配置文件修改成功!");

} catch (IOException e) {

eprintStackTrace();

}

}

}

以上代码将打开"/usr/local/freeswitch/conf/sip_profiles/externalxml"文件,并查找包含字符串"<param name="ext-rtp-ip""的行,并将其替换为"<param name="ext-rtp-ip" value="1921681100"/>"。然后,它将修改后的内容写回到同一文件中。

请注意,在实际 *** 作中,您需要根据您的实际需求修改代码中的文件路径和替换字符串,以确保代码能够正确地修改您想要修改的配置文件。此外,您还需要确保您的应用程序具有足够的权限来修改配置文件。

我记得InputStreamReader和OutputStreamWriter不是可以设置编码的吗?

方式应该是:OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");

InputStreamReader reader = new InputStreamReader(fip, "UTF-8");

这样还乱码?那就二进制读取就行了。

记得保证整个编译环境编码统一。

两种办法 第一: File f = new File(thisgetClass()getResource("/")getPath());

f = new File(fgetPath() + "/conf/configproperties");

注:fgetPath()即为当class所在的绝对路径。如:c:\javasrc\web-inf\classes

然后,对文件对象进行处理,就能把配置信息读取出来了,但是加入如上class被打包成jar文件,那么,在程序执行到这里时,就会无法找到配置文件,那么该如何处理呢?

处理方法如下:

String s_config="conf/configproperties";

File file= new File(StringvalueOf(ClassLoadergetSystemResource(config)));

String filepaths= filegetPath(); 第二:用JarFile JarFile jarFile = new JarFile("thefilejar"); 这个是专门用来读jar文件的

以上就是关于java 修改系统配置文件内容全部的内容,包括:java 修改系统配置文件内容、java中action如何获得客户端文件的路径、关于Java打包路径问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9613991.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存