小编Din*_*esh的帖子

长时间打开和关闭相同文件多次打开和关闭打开文件

File无论何时在JTextArea字段中发生任何内容更改,我都会写信.我决定每次根据更改事件打开和关闭文件内容.

就像是 ,

public void addToLogFile(String changeContent) {
    try {
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(currentLogFile,true)));
        pw.print(changeContent);
        pw.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为可能是在初始阶段打开它并在需要时转储内容,而不是每次打开和关闭文件.最后在结束阶段关闭它.

在计划的初始阶段:

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(currentLogFile,true)));
Run Code Online (Sandbox Code Playgroud)

然后在代码中的某处,无论何时需要,

pw.print(changeContent); // Most frequent usage
Run Code Online (Sandbox Code Playgroud)

在计划的最后阶段:

pw.close();
Run Code Online (Sandbox Code Playgroud)

哪一个会更有效率?在什么条件下,我必须选择一个吗?

java performance file

6
推荐指数
1
解决办法
2358
查看次数

重定向ps命令的输出,获取进程ID并使用shell脚本终止该进程

我想编写一个shell脚本来查找给定用户的运行进程,并通过获取相应的进程ID来终止进程.

就像是

ps -ef | grep dinesh
Run Code Online (Sandbox Code Playgroud)

在此之后,我得到如下输出

dinesh 19985 19890  0 11:35 pts/552  00:00:00 grep dinesh
Run Code Online (Sandbox Code Playgroud)

这里19985是进程ID.我想杀死那个过程.

如何使用脚本实现此目的?

我必须解析ps命令输出并获取进程ID

提前致谢.

shell

5
推荐指数
2
解决办法
6328
查看次数

带有标准输出和文件记录器的 Zerolog 在文件中添加了额外的消息字段

zerolog在我的 go 项目中使用包,我需要将内容记录在文件和stdout. 我从这里参考的。我所做的唯一更改是,logger我没有创建名为 的新变量,而是直接影响全局记录器。

runLogFile, _ := os.OpenFile(
        "myapp.log",
        os.O_APPEND|os.O_CREATE|os.O_WRONLY,
        0664,
    )
fileLogger := zerolog.New(runLogFile).With().Logger()
multi := zerolog.MultiLevelWriter(os.Stdout, fileLogger)
log.Logger = zerolog.New(multi).With().Timestamp().Logger()

log.Info().Msg("Hello World!")
Run Code Online (Sandbox Code Playgroud)

它在标准输出中生成如下所示的输出,这是预期的。

{"level":"info","time":"2022-09-15T08:10:28-04:00","message":"Hello World!"}
Run Code Online (Sandbox Code Playgroud)

但是,文件内容因额外的消息字段而变得混乱,该消息字段再次包装了上述输出。

{"message":"{\"level\":\"info\",\"time\":\"2022-09-15T08:10:28-04:00\",\"message\":\"Hello World!\"}"}
Run Code Online (Sandbox Code Playgroud)

如何强制 Zerolog 记录内容而无需附加消息字段?

go zerolog

5
推荐指数
1
解决办法
3428
查看次数

使用Valgrind检查后,如何查找和删除C程序中的内存泄漏和错误

用Valgrind检查我的代码后,它显示了很多错误消息和信息,我不确定如何查找和删除代码中的错误?

我的源代码如下.

myheader.h

 #include<stdlib.h>
 #define MAX 15
 typedef enum
 {
  METROPOLITAN_AREA,
  TOURIST_AREA,
 }area_type_t;

 typedef struct
 {
  int population;
  int area;
 }metropolitan_t;

 typedef struct
 {
  char *type_place;
  char *near_airport;
 }tourist_t;

 typedef struct
 {
  char *name;
  char *country;
  area_type_t area_t;
  union
  {
      metropolitan_t metro;
      tourist_t   tourist;
  }u;

 }*place_t;
 extern void get_input(place_t);
Run Code Online (Sandbox Code Playgroud)

getinput.c

#include<myheader.h>
void get_input(place_t place)
{
 int check=1;
 int num,i,ch;
 printf("\nEnter the no of records : \n");
 scanf("%d",&num);
 place=(place_t)malloc(sizeof(place_t)*num);
 if(NULL==place)
 {
  printf("\nMemory allocation failed\n");
  exit(1);
 }
 for(i=0;i<num;i++)
 {
  printf("\nEnter the place …
Run Code Online (Sandbox Code Playgroud)

c malloc valgrind memory-leaks

4
推荐指数
1
解决办法
3040
查看次数

如何使用Apache HttpClient发送XML POST请求?

我想做格式的HTTP POST,如下所示,

<?xml version="1.0" encoding="UTF-8" ?>
<authRequest>
 <username>someusernamehere</username>
 <password>somepasswordhere</password>
</authRequest>
Run Code Online (Sandbox Code Playgroud)

对于任何基于登录的POST,我通常使用以下机制,

HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        HttpPost httppost = new HttpPost("http://mysite.com/login");
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("username", "stackoverflow"));
        formparams.add(new BasicNameValuePair("password", "12345"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)

但是通过这种方式,POST数据看起来像,

username=stackoverflow&password=12345
Run Code Online (Sandbox Code Playgroud)

如何根据我上面提到的指定XML格式格式化此请求?

提前致谢.

java http-post apache-commons-httpclient

4
推荐指数
1
解决办法
1万
查看次数

如何从shell脚本运行TCL脚本?

我是TCL脚本和shell脚本的新手.我想从shell脚本调用TCL脚本.我试过如下.

#!/bin/sh

for i in {1..5}
do
   my_script
   test_script
done
Run Code Online (Sandbox Code Playgroud)

如果我运行脚本,它会抛出如下错误,

./sample.sh: line 5: my_script: command not found
./sample.sh: line 5: test_script: command not found
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?

提前致谢.

shell tcl

4
推荐指数
2
解决办法
4万
查看次数

C编程中如何实现优先队列?

我需要使用单链表在 C 编程中实现优先级队列。

我对优先队列没有明确的概念。我用谷歌搜索,但没有完全理解我发现了什么。我的理解是优先级队列是一个元素按优先级排序的队列。列表中的插入根据元素优先级定位在列表中。

可以说,我们有以下场景。(注意:我认为,更高的价值具有更高的优先级):

Element-->2 (priority=2)  (Now in position 0)
Run Code Online (Sandbox Code Playgroud)

如果需要插入另一个元素,请说明Element-->3 (priority=3)哪个具有更高的优先级。

我可以移动前一个元素 ,Element-->2 (priority=2)然后Element-->3 (priority=3)在位置 0插入这个新元素,并Element-->2 (priority=2)移动到列表中的位置 1。

现在名单变成了,

Element-->3 (priority=3) followed by Element-->2 (priority=2)
Run Code Online (Sandbox Code Playgroud)

同样,在插入的基础上,我是否必须将列表中的所有元素都移动?

这样对吗?

c linked-list priority-queue data-structures

3
推荐指数
1
解决办法
2万
查看次数

为什么close()系统调用刷新输出?

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
int main(int argc,char *argv[])
{
  int oldfd;
  int newfd;
  if(argc!=2)
  {
    printf("Usgae : %s file_name\n",argv[0]);
    exit(0);
  }
 oldfd=open(argv[1],O_RDWR|O_APPEND,S_IRWXU); // Opening the file in Read/Write mode
 if (-1 == oldfd)
 {
  perror("Error opening file");
  exit(0);
 }
 close(1); // closing stdout 
 newfd=dup(oldfd); //Now this newfd holds the value 1 
 close(oldfd); //closing the oldfd
 printf("\nStack Overflow"); //Now this printf will print content into the file as stdout closed already
 close(newfd);// closing newfd 
 return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c unix dup

3
推荐指数
1
解决办法
1268
查看次数

监听器在JTable中添加/删除行

是否有任何可用的事件在从JTable添加/删除行后会被触发?

java swing jtable

3
推荐指数
1
解决办法
2869
查看次数

如果我使用"realloc()",为什么Valgrind会产生"无效免费或删除"?

使用realloc()时,我用valgrind检查了它,如下所示

 valgrind --tool=memcheck --leak-check=yes --show-reachable=yes a.out
Run Code Online (Sandbox Code Playgroud)

而valgrind产生的错误信息是

==6402== Memcheck, a memory error detector.
==6402== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==6402== Using LibVEX rev 1575, a library for dynamic binary translation.
==6402== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==6402== Using valgrind-3.1.1, a dynamic binary instrumentation framework.
==6402== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==6402== For more details, rerun with: -v
==6402==
dinesh
vignesh
==6402== Invalid free() / …
Run Code Online (Sandbox Code Playgroud)

c valgrind realloc

2
推荐指数
2
解决办法
2544
查看次数